4

Please bear with me. I have no experience using AAD/PS.

I am just playing around trying to generate a list of users and their managers.

What I have done is something like this:

$UserId = (Get-AzureADUser -All $true).ObjectId
$bla = foreach ($User in $UserId)
    {
        write-output $User
        Get-AzureADUserManager -ObjectId $User
    }
$bla | Out-File "C:\Temp\Test.txt"

I populate $UserId with a users ObjectId and then search manager for that user user the Get-AzureADUserManager.

How can I change it to generate a list that includes the following data:

UsersObjectId UserUPN UserType ManagersObjectId ManagerUPN ManagerUserType

I also want it to include all UserObjectIds that does not have any manager set (include null).

Any ideas how to achieve this?

1 Answer 1

15

This should do what you want:

$output = @()

$users = Get-AzureADUser -All $true

foreach ($user in $users) {
    $manager = Get-AzureADUserManager -ObjectId $user.ObjectId

    $data = New-Object -TypeName psobject

    $data | Add-Member -MemberType NoteProperty -Name UsersObjectId -Value $user.ObjectId
    $data | Add-Member -MemberType NoteProperty -Name UserUPN -Value $user.UserPrincipalName
    $data | Add-Member -MemberType NoteProperty -Name UserType -Value $user.UserType
    $data | Add-Member -MemberType NoteProperty -Name ManagersObjectId -Value $manager.ObjectId
    $data | Add-Member -MemberType NoteProperty -Name ManagerUPN -Value $manager.UserPrincipalName
    $data | Add-Member -MemberType NoteProperty -Name ManagerUserType -Value $manager.UserType

    $output += $data

}

$output | Export-Csv -Path output.csv -NoTypeInformation

It will create a CSV file with columns equal to the data you are looking for, where each row represents a user from the original query.

This also works if Get-AzureADUserManager returns null.

1
  • 1
    Awesome + just had to include $true on the Get-AzureADUser -All and it works like a charm. Now I understand this better and can expand, thanks Shawn! Feb 14, 2018 at 8:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.