Skip to content

Instantly share code, notes, and snippets.

@MauricioZa
Last active May 13, 2022 19:47
Show Gist options
  • Select an option

  • Save MauricioZa/8351938c8583b4ce6aef55d6126e6841 to your computer and use it in GitHub Desktop.

Select an option

Save MauricioZa/8351938c8583b4ce6aef55d6126e6841 to your computer and use it in GitHub Desktop.
# Enter your Azure AD tenant ID. Replace with your own values.
$TenantId = "e65fxxxx-xxxx-xxxx-xxxx-aca6737299f3"
# Enter the path of the csv file to export users. Replace with your own values.
$csvPath = "C:\myfolder\users.csv"
# Set execution policy
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# OPTIONAL: Install modules (if you do not have them already installed)
Install-Module azuread -Force -Scope CurrentUser
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
# Import necessary modules to current session
Import-Module azuread
Import-Module az
# Connect to your AD tenant from Powershell. A login screen will appear. (It might be behind the active screen).
Connect-AzureAD -TenantId $TenantId
# Run this tio retrieve the group you want to get the users from.
$group = Get-AzureADGroup -SearchString "myfriends"
$group.ObjectId
# List user properties in the screen.
Get-AzureADGroupMember -ObjectId $group.ObjectId | Select-Object DisplayName,UserPrincipalName,ObjectId,UsageLocation,Country | Format-Table -AutoSize
# Export user properties to a CSV file.
Get-AzureADGroupMember -ObjectId $group.ObjectId | Select-Object DisplayName,UserPrincipalName,ObjectId,UsageLocation,Country | Export-CSV $csvPath -NoTypeInformation
# List user properties in the screen.
# Get-AzureADUser | Select-Object DisplayName,UserPrincipalName,ObjectId,UsageLocation,Country | Format-Table -AutoSize
# Export user properties to a CSV file.
#Get-AzureADUser | Select-Object DisplayName,UserPrincipalName,ObjectId,UsageLocation,Country | Export-CSV $csvPath -NoTypeInformation
# ProTip: In the two cmdlets above, you can increase the properties that are shown for each user.
# Just add the desired user properties you want to show after the "Select-Object" piece above.
# If you want to know all the properties available for each user, execute the 2 commands below.
# This will retrieve all users:
# $Users = Get-AzureADUser
# And this will show you the properties that are available. Run it and see the 1st column.
# $Users | gm
# Get-AzureADGroup -ObjectId $group.ObjectId
# $group = Get-AzureADGroup -ObjectId $group.ObjectId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment