Skip to content

Get-O365User

Azure AD: Lists all users in the tenant

#Requires -Version 5.1
#Requires -Modules AzureAD

[CmdletBinding()]
Param(
    [string]$SearchString
)

Process {
    try {
        $users = Get-AzureADUser -All $true -SearchString $SearchString -ErrorAction Stop | Select-Object DisplayName, ObjectID, UserPrincipalName, AccountEnabled -Unique | Sort-Object -Property DisplayName

        if ($null -eq $users -or $users.Count -eq 0) {
            Write-Output "No users found"
            return
        }

        foreach ($usr in $users) {
            [PSCustomObject]@{
                Timestamp         = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
                DisplayName       = $usr.DisplayName
                ObjectId          = $usr.ObjectID
                UserPrincipalName = $usr.UserPrincipalName
                AccountEnabled    = $usr.AccountEnabled
            }
        }
    }
    catch { throw }
}

Optional string to filter users by display name or UPN

An interactive directory of PowerShell scripts.