Skip to content

Set-O365UserProperty

Azure AD: Modifies user properties

#Requires -Version 5.1
#Requires -Modules AzureAD

[CmdletBinding(DefaultParameterSetName = "UserName")]
Param(
    [Parameter(Mandatory = $true, ParameterSetName = "UserObjectId")]
    [guid]$UserObjectId,

    [Parameter(Mandatory = $true, ParameterSetName = "UserName")]
    [string]$UserName,

    [Parameter(ParameterSetName = "UserName")]
    [Parameter(ParameterSetName = "UserObjectId")]
    [string]$DisplayName,

    [Parameter(ParameterSetName = "UserName")]
    [Parameter(ParameterSetName = "UserObjectId")]
    [string]$FirstName,

    [Parameter(ParameterSetName = "UserName")]
    [Parameter(ParameterSetName = "UserObjectId")]
    [string]$LastName,

    [Parameter(ParameterSetName = "UserName")]
    [Parameter(ParameterSetName = "UserObjectId")]
    [string]$PostalCode,

    [Parameter(ParameterSetName = "UserName")]
    [Parameter(ParameterSetName = "UserObjectId")]
    [string]$City,

    [Parameter(ParameterSetName = "UserName")]
    [Parameter(ParameterSetName = "UserObjectId")]
    [string]$Street,

    [Parameter(ParameterSetName = "UserName")]
    [Parameter(ParameterSetName = "UserObjectId")]
    [string]$PhoneNumber,

    [Parameter(ParameterSetName = "UserName")]
    [Parameter(ParameterSetName = "UserObjectId")]
    [string]$MobilePhone,

    [Parameter(ParameterSetName = "UserName")]
    [Parameter(ParameterSetName = "UserObjectId")]
    [string]$Department,

    [Parameter(ParameterSetName = "UserName")]
    [Parameter(ParameterSetName = "UserObjectId")]
    [bool]$Enabled
)

Process {
    try {
        if ($PSCmdlet.ParameterSetName -eq "UserObjectId") {
            $usr = Get-AzureADUser -ObjectId $UserObjectId -ErrorAction Stop | Select-Object ObjectID, DisplayName
        }
        else {
            $usr = Get-AzureADUser -All $true -ErrorAction Stop | Where-Object { ($_.DisplayName -eq $UserName) -or ($_.UserPrincipalName -eq $UserName) } | Select-Object ObjectID, DisplayName
        }

        if ($null -eq $usr) { throw "User not found" }

        if ($PSBoundParameters.ContainsKey('DisplayName')) { $null = Set-AzureADUser -ObjectId $usr.ObjectId -DisplayName $DisplayName -ErrorAction Stop }
        if ($PSBoundParameters.ContainsKey('FirstName')) { $null = Set-AzureADUser -ObjectId $usr.ObjectId -GivenName $FirstName -ErrorAction Stop }
        if ($PSBoundParameters.ContainsKey('LastName')) { $null = Set-AzureADUser -ObjectId $usr.ObjectId -Surname $LastName -ErrorAction Stop }
        if ($PSBoundParameters.ContainsKey('PostalCode')) { $null = Set-AzureADUser -ObjectId $usr.ObjectId -PostalCode $PostalCode -ErrorAction Stop }
        if ($PSBoundParameters.ContainsKey('City')) { $null = Set-AzureADUser -ObjectId $usr.ObjectId -City $City -ErrorAction Stop }
        if ($PSBoundParameters.ContainsKey('Street')) { $null = Set-AzureADUser -ObjectId $usr.ObjectId -StreetAddress $Street -ErrorAction Stop }
        if ($PSBoundParameters.ContainsKey('PhoneNumber')) { $null = Set-AzureADUser -ObjectId $usr.ObjectId -TelephoneNumber $PhoneNumber -ErrorAction Stop }
        if ($PSBoundParameters.ContainsKey('MobilePhone')) { $null = Set-AzureADUser -ObjectId $usr.ObjectId -Mobile $MobilePhone -ErrorAction Stop }
        if ($PSBoundParameters.ContainsKey('Department')) { $null = Set-AzureADUser -ObjectId $usr.ObjectId -Department $Department -ErrorAction Stop }
        if ($PSBoundParameters.ContainsKey('Enabled')) { $null = Set-AzureADUser -ObjectId $usr.ObjectId -AccountEnabled $Enabled -ErrorAction Stop }

        $updated = Get-AzureADUser -ObjectId $usr.ObjectId -ErrorAction Stop | Select-Object *
        $updated | Add-Member -NotePropertyName Timestamp -NotePropertyValue (Get-Date -Format "yyyy-MM-dd HH:mm:ss") -PassThru -Force
    }
    catch { throw }
}

Unique object ID of the user to modify

Display name or UPN of the user to modify

New display name for 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 of the user

Department of the user

Whether the account is enabled for sign-in

An interactive directory of PowerShell scripts.