question

RisingFlight-7863 avatar image
1 Vote"
RisingFlight-7863 asked henrycrist-0272 published

Azure AD Export AD groups

When i go to Azure Active Directory

Home-->Contoso-->users-->Ben
under groups(Preview) i can see all groups for user Ben i.e mail enabled security, security, Distribution,Microsoft 365.
i want to export Group Names, Group Types, Email to csv file for this user. how do i do it.

  1. If i just need to export Distribution groups how do i do.

  2. if i have users in csv file how can i import the csv file and export the output(groups) to csv file from Azure AD powershell

users
john1@mydomain.com
John2@mydomain.com

windows-serverazure-active-directorywindows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
2 Votes"
AndreasBaumgarten answered

Please give it a try:

 $users = Get-Content -Path "$userFile" 
  ForEach ($user in $users)
         {
             $memberships = Get-AzureADUserMembership -ObjectId $user | Where-Object {$_.MailEnabled -eq $true}
             foreach ($membership in $memberships)
                 {
                 $membershipDisplayName =  $membership.Displayname
                 $membershipMailEnabled = $membership.MailEnabled
                 $membershipObjectType = $membership.ObjectType
                 $membershipSecurityEnabled = $membership.SecurityEnabled
                 $membershipMail = $membership.Mail
                    
                 $out = "$user,$membershipDisplayName,$membershipMailEnabled,$membershipObjectType,$membershipSecurityEnabled,$membershipMail"
                 $out | Out-File -FilePath $outputFile -Append
                 }
         }


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
1 Vote"
AndreasBaumgarten answered AndreasBaumgarten edited

Via Azure Portal:
Export AAD groups works the same like export AAD users:
https://docs.microsoft.com/en-us/azure/active-directory/enterprise-users/users-bulk-download
You can use the filter before export if required.

Import AAD users from CSV:
https://docs.microsoft.com/en-us/azure/active-directory/enterprise-users/users-bulk-add

Via PowerShell:
Import users
https://docs.microsoft.com/en-us/powershell/azure/active-directory/importing-data?view=azureadps-2.0

Export groups:
https://docs.microsoft.com/de-de/azure/active-directory/enterprise-users/groups-settings-v2-cmdlets


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RisingFlight-7863 avatar image
1 Vote"
RisingFlight-7863 answered RisingFlight-7863 edited

If i have users in csv file will the below syntax work for me

Names
u1@mydomain.com
u2@mydomain.com

how can i add mail enabled set to true in this synax so that i can pull only distribution groups, unified groups and mail enabled security group.

import-csv c:\temp\users.csv | % {Get-AzureADUserMembership -ObjectID -identity $_.Names | Select-Object DisplayName, MailEnabled, ObjectType, SecurityEnabled, Mail | Export-Csv -Path C:\Temp\Data.CSV -NoTypeInformation}

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
1 Vote"
AndreasBaumgarten answered

You can use Where-Object for filtering the Get-AzureADUserMembership output.

Just an example (not tested):

 Get-AzureADUserMembership -ObjectID -identity $_.Names | Where {$_.MailEnabled -eq 'true'} # Where MailEnabled is "true"


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RisingFlight-7863 avatar image
1 Vote"
RisingFlight-7863 answered RisingFlight-7863 edited

when i use the below syntax i am getting error

Names
u1@mydomain.com
u2@mydomain.com

import-csv C:\Temp\users.csv | % {Get-AzureADUserMembership -ObjectID -identity $.Names | Where {$.MailEnabled -eq 'true'}| Select-Object DisplayName, MailEnabled, ObjectType, SecurityEnabled, Mail | Export-Csv -Path C:\Temp\Data.csv -NoTypeInformation}

Get-AzureADUserMembership : A positional parameter cannot be found that accepts argument
'u1@mydomain.com'.
At line:1 char:59
+ import-csv C:\Temp\users.csv | % {Get-AzureADUserMembers ...
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-AzureADUserMembership], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Open.AzureAD16.PowerShell.GetUserMemberships

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
1 Vote"
AndreasBaumgarten answered AndreasBaumgarten edited

Not tested:

 import-csv C:\Temp\users.csv | Foreach-Object {Get-AzureADUserMembership -ObjectId $_ | Where {$_.MailEnabled -eq $true} | Select-Object DisplayName, MailEnabled, ObjectType, SecurityEnabled, Mail | Export-Csv -Path C:\Temp\Data.csv -NoTypeInformation}


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RisingFlight-7863 avatar image
1 Vote"
RisingFlight-7863 answered

i have tried but getting the below error

Get-AzureADUserMembership : Error occurred while executing GetUserMemberships
Code: Request_ResourceNotFound
Message: Resource '@{Names=u1@mydomain.com}' does not exist or one of its queried reference-property
objects are not present.
RequestId: 8764a89ad-f199-9876-abe6-f2c09c531b78
HttpStatusCode: NotFound
HttpStatusDescription: Not Found
HttpResponseStatus: Completed
At line:1 char:72
+ import-csv C:\temp\users.csv | Foreach-Object {Get-Azure ...
+ ~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-AzureADUserMembership], ApiException
+ FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.GetUser
Memberships

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
1 Vote"
AndreasBaumgarten answered

This is working here:

 $user = "peter@something.xyz" , "paul@something.xyz"
    
 $user |ForEach-Object {Get-AzureADUserMembership -ObjectId $_} |  Where-Object {$_.MailEnabled -eq $true}

Maybe for testing you can just replace the usernames in $user and give it a try.


Your csv file only contains the usernames like this?

 u1@mydomain.com
 u2@mydomain.com


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RisingFlight-7863 avatar image
1 Vote"
RisingFlight-7863 answered

I am getting the output i have used the below syntax. my csv file is in below format. i am getting output as groups i am unable to get displayname of the user or userprincipalname. i cannot know which user has which groups

 u1@mydomain.com
 u2@mydomain.com

 $user = Get-Content C:\temp\users.csv 
 $user |ForEach-Object {Get-AzureADUserMembership -ObjectId $_} |  Where-Object {$_.MailEnabled -eq $true} | Select-Object userprincipalname,DisplayName, MailEnabled, ObjectType, SecurityEnabled, Mail | Export-Csv -Path  C:\Temp\Data.csv -NoTypeInformation



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SagarGohil avatar image
1 Vote"
SagarGohil answered HRaja-6229 commented

Hi @RisingFlight-7863, Thank you for reaching out to us.

Based on the above query, I would like to shared the below script which I have created for the above ask.

$i=1
$j=0
$userdata=Import-Csv <file path where in we have the user data in CSV> #
Please make sure that we have ID (Object ID) and user name (Userprincipal) attributed in the CSV.
$userdata | foreach {
Get-AzureADUserMembership -ObjectId $.id | Select-Object displayname, securityenabled, mailenabled, mail, objecttype,@{Name="UPN";Expression={$userdata[$j].userprincipalname}} | where {$.objecttype -NE "Role"} |Export-Csv C:\Users\Userdetails.csv -Append
$j++
}

This will get the output for each users in the file we imported along with below details:

  • Display name of the group

  • SecurityEnabled

  • Mailenabled

  • ObjectType

  • UPN

I have tested the above script in my test tenant, and it is giving me appropriate output.


Please "Accept the answer" if the information helped you. This will help us and others in the community as well.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@SagarGohil how is the export file set? and are you elevating any Azure role?

0 Votes 0 ·