使用 PowerShell 設定 Microsoft 365 用戶帳戶屬性

本文適用於 Microsoft 365 企業版和 Office 365 企業版。

您可以使用 Microsoft 365 系統管理中心 來設定 Microsoft 365 租使用者使用者帳戶的屬性。 在 PowerShell 中,您也可以這麼做,再加上一些您無法在系統管理中心執行的其他動作。

使用 Microsoft Graph PowerShell 設定 Microsoft 365 使用者帳戶屬性

注意事項

Azure Active Directory 模組正由 Microsoft Graph PowerShell SDK 取代。 您可以使用 Microsoft Graph PowerShell SDK 來存取所有的 Microsoft Graph API。 如需詳細資訊,請參閱 開始使用 Microsoft Graph PowerShell SDK

首先,使用 Microsoft Entra DC 系統管理員雲端應用程式 管理員全域系統管理員帳戶來連線到您的 Microsoft 365 租使用者。 本文中的 Cmdlet 需要許可權範圍 User.ReadWrite.All'List subscribedSkus' 圖形 API 參考頁面中所列的其中一個其他許可權。 本文中的某些命令可能需要不同的許可權範圍,在此情況下,這會在相關章節中註明。

Connect-MgGraph -Scopes "User.ReadWrite.All"

變更特定用戶帳戶的屬性

您可以使用 -ObjectID 參數來識別帳戶,並使用其他參數來設定或變更特定屬性。 以下是最常見的參數清單:

  • -Department “<department name>”

  • -DisplayName “<full user name>”

  • -FacsimilieTelephoneNumber “<fax number>”

  • -GivenName “<user first name>”

  • -Surname “<user last name>”

  • -行動裝置「<行動電話號碼>」

  • -JobTitle “<job title>”

  • -PreferredLanguage “<language>”

  • -StreetAddress “<street address>”

  • -City “<city name>”

  • -State “<state name>”

  • -PostalCode “<postal code>”

  • -Country “<country name>”

  • -PhoneNumber “<office phone number>”

  • -UsageLocation“<2 個字元的國家或地區代碼>”

    這是 ISO 3166-1 alpha-2 (A2) 兩個字母的國家或地區代碼。

注意事項

您必須先指派使用位置,才能將授權指派給用戶帳戶。

若要顯示使用者帳戶的用戶主體名稱 (UPN) ,請執行下列命令。

Get-MgUser -All | Sort-Object UserPrincipalName | Select-Object UserPrincipalName | More

此指令會指示 PowerShell:

  1. 取得 Get-MgUser) (使用者帳戶的所有資訊,並將其傳送至下一個命令 (|) 。

  2. 依字母順序排序 UPN 清單 (排序 UserPrincipalName) ,並將它傳送至下一個命令 (|) 。

  3. 只顯示每個帳戶的 UPN 屬性 (選取 UserPrincipalName) 。

  4. 在 [ 更多) ] (一次顯示一個畫面。

若要根據帳戶的顯示名稱來顯示帳戶的 UPN (名字和姓氏) ,請執行下列命令。 填入 $userName 變數,並移除 < 和 > 字元:

$userName="<Display name>"
Write-Host (Get-MgUser -All | where {$_.DisplayName -eq $userName}).UserPrincipalName

本範例會顯示顯示名稱 為 Caleb Sills 之用戶帳戶的 UPN。

$userName="Caleb Sills"
Write-Host (Get-MgUser -All | where {$_.DisplayName -eq $userName}).UserPrincipalName

藉由使用 $upn 變數,您可以根據個別帳戶的顯示名稱進行變更。 以下是將 Belinda Newman 的使用位置設定為法國的範例。 但它會指定她的顯示名稱,而不是UPN:

$userName="Belinda Newman"
$upn=(Get-MgUser | where {$_.DisplayName -eq $userName}).UserPrincipalName
Update-MgUser -UserId $upn -UsageLocation "FR"

變更所有用戶帳戶的屬性

若要變更所有使用者的屬性,您可以使用 Get-MgUserUpdate-MgUser Cmdlet 的組合。 下列範例會將所有使用者的使用位置變更為 法國

Get-MgUser | ForEach-Object { Update-MgUser -UserId $_.Id -UsageLocation "FR" }

此指令會指示 PowerShell:

  1. (Get-MgUser) 取得用戶帳戶的所有資訊,並將其傳送至下一個命令 (|) 。

  2. 將使用者位置設定為法國 (Update-MgUser -UsageLocation FR) 。

變更一組特定用戶帳戶的屬性

若要變更特定用戶帳戶集的屬性,您可以使用 Get-MgUserWhereUpdate-MgUser Cmdlet 的組合。 下列範例會將會計部門中所有使用者的使用位置變更為 法國

Get-MgUser -All | Where-Object {$_.Department -eq "Accounting"} | ForEach-Object {Update-MgUser -UserId $_.Id -UsageLocation "FR"}

此指令會指示 PowerShell:

  1. 取得 Get-MgUser) (使用者帳戶的所有資訊,並將其傳送至下一個命令 (|) 。

  2. 尋找其 Department 屬性設定為 “Accounting” 的所有使用者帳戶 (Where {$_。Department -eq “Accounting”}) ,並將產生的信息傳送至下一個命令 (|) 。

  3. 將使用者位置設定為法國 (Update-MgUser -UsageLocation FR) 。

另請參閱

以 PowerShell 管理 Microsoft 365 使用者帳戶、授權和群組

使用 PowerShell 管理 Microsoft 365

開始使用適用於 Microsoft 365 的 PowerShell