Skip to content

New-MsOUser

MSOnline: Create a user in Azure AD

#Requires -Version 5.1

[CmdletBinding()]
Param(
    [Parameter(Mandatory = $true)]
    [string]$UserPrincipalName,
    [securestring]$Password,
    [Parameter(Mandatory = $true)]
    [string]$DisplayName,
    [string]$FirstName,
    [string]$LastName,
    [string]$PostalCode,
    [string]$City,
    [string]$Street,
    [string]$PhoneNumber,
    [string]$MobilePhone,
    [string]$Department,
    [string]$Title,
    [string]$Country,
    [string]$State,
    [string]$UsageLocation,
    [switch]$ForceChangePassword,
    [guid]$TenantId
)

Process {
    try {
        [hashtable]$newArgs = @{'ErrorAction' = 'Stop'; 'UserPrincipalName' = $UserPrincipalName; 'DisplayName' = $DisplayName; 'TenantId' = $TenantId}
        if ($null -ne $Password) { $newArgs.Add('Password', (New-Object System.Net.NetworkCredential('', $Password)).Password) }
        if (-not [System.String]::IsNullOrWhiteSpace($FirstName)) { $newArgs.Add('FirstName', $FirstName) }
        if (-not [System.String]::IsNullOrWhiteSpace($LastName)) { $newArgs.Add('LastName', $LastName) }
        if (-not [System.String]::IsNullOrWhiteSpace($UsageLocation)) { $newArgs.Add('UsageLocation', $UsageLocation) }
        if ($ForceChangePassword) { $newArgs.Add('ForceChangePassword', $true) }

        $result = New-MsolUser @newArgs | Select-Object *
        if ($null -eq $result) { Write-Output "Failed to create user"; return }
        $result | Add-Member -NotePropertyName Timestamp -NotePropertyValue (Get-Date -Format 'yyyy-MM-dd HH:mm:ss') -PassThru -Force
    }
    catch { throw }
}

User ID for this user

New password for the user

Display name of the user

First name of the user

Last name of the user

Postal code of the user

City of the user

Street address of the user

Phone number of the user

Mobile phone number

Department of the user

Job title of the user

Country of the user

State or province

Usage location (ISO country code)

Off

User must change password on next sign-in

Unique ID of the tenant

An interactive directory of PowerShell scripts.