Skip to content

Commit 39b2ba7

Browse files
committed
Add script
1 parent e91b0e3 commit 39b2ba7

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Microsoft 365/GetAllTenantUsers.ps1

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# PowerShell script to get all users in Microsoft 365 tenant using Microsoft Graph API
2+
# This script requires the Microsoft.Graph PowerShell module
3+
4+
# Check if Microsoft.Graph module is installed and install if needed
5+
if (-not (Get-Module -ListAvailable -Name Microsoft.Graph.Users)) {
6+
Write-Host "Microsoft.Graph.Users module not found. Installing..."
7+
Install-Module Microsoft.Graph.Users -Scope CurrentUser -Force
8+
}
9+
10+
# Import required modules
11+
Import-Module Microsoft.Graph.Users
12+
13+
# Connect to Microsoft Graph
14+
Connect-MgGraph -Scopes "User.Read.All" -NoWelcome
15+
16+
# Get all users from the tenant
17+
$users = Get-MgUser -All
18+
19+
# Display user information
20+
$users | Select-Object DisplayName, UserPrincipalName, Id, JobTitle, Department | Format-Table -AutoSize
21+
22+
# Export to CSV file (Optional)
23+
$csvPath = Join-Path -Path $PSScriptRoot -ChildPath "TenantUsers_$(Get-Date -Format 'yyyyMMdd').csv"
24+
$users | Select-Object DisplayName, UserPrincipalName, Id, Mail, JobTitle, Department, AccountEnabled | Export-Csv -Path $csvPath -NoTypeInformation
25+
26+
Write-Host "Total users found: $($users.Count)"
27+
Write-Host "Users exported to: $csvPath"
28+
29+
# Disconnect from Microsoft Graph
30+
Disconnect-MgGraph

0 commit comments

Comments
 (0)