List user information with PowerShell and Microsoft Graph from Azure Active Directory!

%3CLINGO-SUB%20id%3D%22lingo-sub-2555959%22%20slang%3D%22en-US%22%3EList%20user%20information%20with%20PowerShell%20and%20Microsoft%20Graph%20from%20Azure%20Active%20Directory!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2555959%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHi%20Azure%20%2F%20Microsoft365%20friends%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20small%20example%20I%20am%20concerned%20with%20how%20information%20can%20be%20collected%20with%20the%20Microsoft%20Graph.%20Really%20nothing%20spectacular%2C%20but%20an%20interesting%20lesson%20for%20me.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20used%20the%20PowerShell%20ISE%20for%20this%20configuration.%20But%20you%20are%20also%20very%20welcome%20to%20use%20Visual%20Studio%20Code%2C%20just%20as%20you%20wish.%20Please%20start%20with%20the%20following%20steps%20to%20begin%20the%20deployment%20(the%20Hashtags%20are%20comments)%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23The%20first%20two%20lines%20have%20nothing%20to%20do%20with%20the%20configuration%2C%20but%20make%20some%20space%20below%20in%20the%20blue%20part%20of%20the%20ISE.%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ESet-Location%20C%3A%5C%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EClear-Host%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Install%20Microsoft%20Graph%20Module%3CBR%20%2F%3E%3CSTRONG%3EInstall-Module%20Microsoft.Graph%20-AllowClobber%20-Force%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Time%20range%3CBR%20%2F%3E%3CSTRONG%3E%24date%20%3D%20(Get-Date).AddDays(-60)%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23A%20variable%20for%20later%20output%3CBR%20%2F%3E%3CSTRONG%3E%24properties%20%3D%20'AccountEnabled'%2C%20'UserPrincipalName'%2C'Id'%2C'CreatedDateTime'%2C'LastPasswordChangeDateTime'%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Connect%20to%20the%20cloud%20(incl.%20necessary%20permissions)%3CBR%20%2F%3E%3CSTRONG%3EConnect-Graph%20-Scopes%20User.Read.All%2C%20Directory.AccessAsUser.All%2C%20User.ReadBasic.All%2C%20User.ReadWrite.All%2C%20Directory.Read.All%2C%20Directory.ReadWrite.All%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23We%20check%20the%20permissions%3CBR%20%2F%3E%3CSTRONG%3E(Get-MgContext).Scopes%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23List%20the%20users%20and%20store%20them%20in%20a%20variable%3CBR%20%2F%3E%3CSTRONG%3E%24mgUsers%20%3D%20Get-MgUser%20-All%20-Select%20%24properties%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Let's%20look%20at%20the%20list%3CBR%20%2F%3E%3CSTRONG%3E%24mgUsers%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23How%20many%20are%20there%3F%3CBR%20%2F%3E%3CSTRONG%3E%24mgUsers.count%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Get-Member%20to%20get%20the%20details%3CBR%20%2F%3E%3CSTRONG%3EGet-MgUser%20%7C%20Get-Member%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Creation%20date%20and%20last%20password%20change%3CBR%20%2F%3E%3CSTRONG%3E%24InfoUsers%20%3D%20%24mgUsers%20%7C%20Where-Object%20%7B%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%24_.CreatedDateTime%20-lt%20%24date%20-and%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%24_.LastPasswordChangeDateTime%20-lt%20%24date%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%7D%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23How%20many%20have%20we%20found%20(No%20longer%20the%20same%20number)%3F%3CBR%20%2F%3E%3CSTRONG%3E%24InfoUsers.count%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23We'll%20take%20a%20look%20at%20it%3CBR%20%2F%3E%3CSTRONG%3E%24InfoUsers%20%7C%20Format-Table%20%24properties%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Remove%20the%20session%3CBR%20%2F%3E%3CSTRONG%3EDisconnect-Graph%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20know%20that%20wasn't%20super%20fancy%20at%20all.%20But%20I%20really%20wanted%20to%20share%20my%20experience%20with%20you.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20hope%20this%20article%20was%20useful.%20Best%20regards%2C%20Tom%20Wechsler%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EP.S.%20All%20scripts%20(%23PowerShell%2C%20Azure%20CLI%2C%20%23Terraform%2C%20%23ARM%2C%20etc.)%20that%20I%20use%20can%20be%20found%20on%20github!%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Ftomwechsler%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Ftomwechsler%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2555959%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EHands-on-Labs%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
MVP

 

Hi Azure / Microsoft365 friends,

 

In this small example I am concerned with how information can be collected with the Microsoft Graph. Really nothing spectacular, but an interesting lesson for me.

 

I used the PowerShell ISE for this configuration. But you are also very welcome to use Visual Studio Code, just as you wish. Please start with the following steps to begin the deployment (the Hashtags are comments):

 

#The first two lines have nothing to do with the configuration, but make some space below in the blue part of the ISE.

Set-Location C:\
Clear-Host

 

#Install Microsoft Graph Module
Install-Module Microsoft.Graph -AllowClobber -Force

 

#Time range
$date = (Get-Date).AddDays(-60)

 

#A variable for later output
$properties = 'AccountEnabled', 'UserPrincipalName','Id','CreatedDateTime','LastPasswordChangeDateTime'

 

#Connect to the cloud (incl. necessary permissions)
Connect-Graph -Scopes User.Read.All, Directory.AccessAsUser.All, User.ReadBasic.All, User.ReadWrite.All, Directory.Read.All, Directory.ReadWrite.All

 

#We check the permissions
(Get-MgContext).Scopes

 

#List the users and store them in a variable
$mgUsers = Get-MgUser -All -Select $properties

 

#Let's look at the list
$mgUsers

 

#How many are there?
$mgUsers.count

 

#Get-Member to get the details
Get-MgUser | Get-Member

 

#Creation date and last password change
$InfoUsers = $mgUsers | Where-Object {
$_.CreatedDateTime -lt $date -and
$_.LastPasswordChangeDateTime -lt $date
}

 

#How many have we found (No longer the same number)?
$InfoUsers.count

 

#We'll take a look at it
$InfoUsers | Format-Table $properties

 

#Remove the session
Disconnect-Graph

 

I know that wasn't super fancy at all. But I really wanted to share my experience with you.

 

I hope this article was useful. Best regards, Tom Wechsler

 

P.S. All scripts (#PowerShell, Azure CLI, #Terraform, #ARM, etc.) that I use can be found on github! https://github.com/tomwechsler

0 Replies