I need some help on the ordering/removal of csv file and its data. The output file looks like this:
@{UserPrincipalName=name.surname@domain.com}
@{UserPrincipalName=name1.surname1@domain.com}
@{UserPrincipalName=name2.surname2@domain.com}
however what I like to get is the following:
'name.surname@domain.com', 'name1.surname1@domain.com', 'name2.surname2@domain.com'
I tried it with replace, trim and other commands however no success so far. Here my AAD scripts which I use to get the users.
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
Thanks for any help.
Simply replace the last line:
Set-Content -path $Output -value $userlist
With:
$userlist |Export-Csv -LiteralPath $output -NoTypeInformation
Export-Csv
will inspect the properties on the input objects, find that they only have a UserPrincipalName
property, and output that as the only column in the resulting CSV file:
UserPrincipalName
name.surname@domain.com
name1.surname1@domain.com
name2.surname2@domain.com
To read the values back into memory, use Import-Csv
:
PS ~> $UPNs = Import-Csv path\to\AAD_Users_only.csv
PS ~> $UPNs
UserPrincipalName
-----------------
name.surname@domain.com
name1.surname1@domain.com
name2.surname2@domain.com