Powershell更改csv文件的格式

s2j5cfk0  于 12个月前  发布在  Shell
关注(0)|答案(1)|浏览(107)

我需要一些关于CSV文件及其数据的订购/删除帮助。输出文件如下所示:

@{[email protected]}
@{UserPrincipalName=name1.surna[email protected]}
@{[email protected]}

但是我喜欢的是以下内容:

'[email protected]', '[email protected]', '[email protected]'

我尝试了替换,修剪和其他命令,但到目前为止没有成功。这里是我的AAD脚本,我用它来获取用户。

Write-Host "Fetching data from Azure Active Directory..."
$cred = New-Object -TypeName PSCredential -argumentlist $username, $pwd
#Connect to M365
Connect-MsolService -Credential $cred
$Users = Get-MsolUser -All | Where-Object { $_.UserType -ne "Guest" }
$userlist = [System.Collections.Generic.List[Object]]::new() # Create output file
ForEach ($User in $Users) {

    $ReportLine = [PSCustomObject] @{
        UserPrincipalName  = $User.UserPrincipalName
    }
                
    $userlist.Add($ReportLine)
    }

$Output = "c:\logging\data\AAD_Users_only.csv"
Set-Content -path $Output -value $userlist

谢谢你的帮助。

2sbarzqh

2sbarzqh1#

只需替换最后一行:

Set-Content -path $Output -value $userlist

有:

$userlist |Export-Csv -LiteralPath $output -NoTypeInformation

Export-Csv将检查输入对象上的属性,发现它们只有一个UserPrincipalName属性,并将其作为结果CSV文件中的唯一列输出:

UserPrincipalName
name.surname@domain.com
[email protected]
[email protected]

要将值读回内存,请使用Import-Csv

PS ~> $UPNs = Import-Csv path\to\AAD_Users_only.csv
PS ~> $UPNs

UserPrincipalName
-----------------
[email protected]
[email protected]
[email protected]

相关问题