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

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