0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.