Guest Users Audit
Exports all guest (external) user accounts in the tenant to CSV.
Requirements
Install-Module Microsoft.Graph.Authentication -Scope CurrentUser
Install-Module Microsoft.Graph.Users -Scope CurrentUserRequires PowerShell 7.0+.
Usage
# Default output
.\guest-users-audit.ps1
# Custom output path
.\guest-users-audit.ps1 -OutputPath "C:\Reports\guests.csv"Script
#requires -Version 7.0
[CmdletBinding()]
param(
[string]$OutputPath = (Join-Path $PSScriptRoot 'guest-users.csv')
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Ensure-Module {
param([Parameter(Mandatory=$true)][string]$Name)
if (-not (Get-Module -ListAvailable -Name $Name)) {
Write-Error "Required module '$Name' not found. Install with: Install-Module $Name -Scope CurrentUser"
exit 1
}
Import-Module $Name -ErrorAction Stop | Out-Null
}
Ensure-Module -Name Microsoft.Graph.Authentication
Ensure-Module -Name Microsoft.Graph.Users
$scopes = @('User.Read.All', 'Directory.Read.All')
Connect-MgGraph -Scopes $scopes | Out-Null
$guests = Get-MgUser -All -Filter "userType eq 'Guest'" -Property 'id,displayName,mail,userPrincipalName,accountEnabled,createdDateTime,userType'
$rows = $guests | ForEach-Object {
[pscustomobject]@{
DisplayName = $_.DisplayName
UserPrincipalName = $_.UserPrincipalName
Mail = $_.Mail
AccountEnabled = $_.AccountEnabled
CreatedDateTime = $_.CreatedDateTime
UserType = $_.UserType
}
}
$rows | Sort-Object CreatedDateTime | Export-Csv -NoTypeInformation -Path $OutputPath
Write-Host "Saved $($rows.Count) rows to $OutputPath" -ForegroundColor Green
Output
Exports a CSV with:
DisplayNameUserPrincipalNameMailAccountEnabledCreatedDateTimeUserType
Graph Permissions
User.Read.AllDirectory.Read.All
Last updated on