This code works when ran on its own, I'm trying to combine both commands output to a single csv, formatted with the correct headers.
$objectIDs = Get-AzureADGroupMember -ObjectId x | select objectid,displayname,mail
$groups = foreach ($objectID in $objectids) {
Get-AzureADUser -ObjectId $objectid.ObjectId | select mail ;
Get-AzureADUserMembership -ObjectId $objectID.ObjectId | Where-Object { $_.displayname -like '*xxxxxxxx*' } | select displayname
}
$groups | Out-File C:\temp\test.csv
Basically, I need the mail and specific group name in a csv.
CodePudding user response:
You can combine the wanted values like below:
$objectIDs = Get-AzureADGroupMember -ObjectId x | Select-Object objectid,displayname,mail
$groups = foreach ($objectID in $objectids) {
$user = Get-AzureADUser -ObjectId $objectid.ObjectId
Get-AzureADUserMembership -ObjectId $objectID.ObjectId | Where-Object { $_.displayname -like '*xxxxxxxx*' } |
Select-Object @{Name = 'email'; Expression = {$user.mail}}, displayname
}
$groups | Export-Csv -Path 'C:\temp\test.csv' -NoTypeInformation
Please note that if the output should be a structured CSV file, you need to use Export-Csv
on the resulting objects instead of Out-File