Must-Know MS Graph PowerShell Cmdlets

Listen to this Post

Microsoft Graph PowerShell is the modern way to manage Microsoft 365, replacing older modules like MSOnline and AzureAD. Here are 10 essential cmdlets to enhance your administrative tasks:

1. Get-MgUser – Retrieve user details.

2. Get-MgGroup – Fetch group information.

3. Get-MgSubscribedSku – Check license assignments.

4. Get-MgAuditLogSignIn – Audit sign-in logs.

5. Get-MgDevice – List registered devices.

6. Set-MgUserLicense – Modify user licenses.

7. New-MgGroup – Create a new group.

8. Remove-MgUser – Delete a user account.

9. Invoke-MgGraphRequest – Run custom Graph API queries.

10. Connect-MgGraph – Authenticate to Microsoft Graph.

πŸ”— Reference: MS Graph PowerShell Cmdlets

You Should Know:

1. Fetching User Reports

 Connect to Microsoft Graph 
Connect-MgGraph -Scopes "User.Read.All"

Get all users 
$users = Get-MgUser -All 
$users | Select-Object DisplayName, UserPrincipalName, Id 

2. Auditing License Assignments

 List all licenses in the tenant 
$licenses = Get-MgSubscribedSku 
$licenses | Select-Object SkuPartNumber, ConsumedUnits 

3. Filtering Active Sign-Ins

 Get recent sign-ins 
$signIns = Get-MgAuditLogSignIn -Top 100 
$signIns | Where-Object { $_.Status.ErrorCode -eq 0 } | Select-Object UserDisplayName, IpAddress 

4. Managing Groups

 Create a new Microsoft 365 group 
New-MgGroup -DisplayName "Security Team" -MailEnabled:$false -SecurityEnabled:$true -MailNickname "SecTeam" 

5. Bulk User License Assignment

 Assign a license to multiple users 
$users = Get-MgUser -Filter "Department eq 'Sales'" 
foreach ($user in $users) { 
Set-MgUserLicense -UserId $user.Id -AddLicenses @{SkuId = "SKU_ID"} -RemoveLicenses @() 
} 

6. Exporting Reports to CSV

 Export all users to CSV 
Get-MgUser -All | Export-Csv -Path "UsersReport.csv" -NoTypeInformation 

7. Running Custom Graph Queries

 Fetch users with a custom query 
Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/users?`$select=displayName,jobTitle" 

What Undercode Say

Microsoft Graph PowerShell is a powerful tool for automating Microsoft 365 administration. Key takeaways:
– Always use `Connect-MgGraph` with least-privilege scopes.
– Filter large datasets with `-Filter` for efficiency.
– Use `Invoke-MgGraphRequest` for advanced queries.

πŸ”— Bonus Scripts: 15 Pre-Built MS Graph PowerShell Scripts

Expected Output:

 Example: Retrieve all users and their licenses 
Connect-MgGraph -Scopes "User.Read.All", "Directory.Read.All" 
$users = Get-MgUser -All 
$users | ForEach-Object { 
$license = Get-MgUserLicenseDetail -UserId $<em>.Id 
[bash]@{ 
User = $</em>.DisplayName 
UPN = $_.UserPrincipalName 
Licenses = $license.SkuPartNumber -join ", " 
} 
} | Export-Csv -Path "UserLicenses.csv" -NoTypeInformation 

Mastering these cmdlets will streamline your Microsoft 365 management tasks efficiently. πŸš€

References:

Reported By: Kavya A – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass βœ…

Join Our Cyber World:

πŸ’¬ Whatsapp | πŸ’¬ TelegramFeatured Image