使用 PowerShell 設定Microsoft 365使用者帳戶屬性
本文適用於 Microsoft 365 企業版和 Office 365 企業版。
您可以使用Microsoft 365 系統管理中心來設定Microsoft 365租使用者使用者帳戶的屬性。 在 PowerShell 中,您也可以這麼做,再加上一些您無法在系統管理中心執行的其他動作。
針對 Graph 模組,請使用 Azure Active Directory PowerShell
若要在 Azure Active Directory PowerShell for Graph 模組中設定使用者帳戶的屬性,請使用 Set-AzureADUser Cmdlet,並指定要設定或變更的屬性。
變更特定使用者帳戶的屬性
您可以使用 -ObjectID 參數來識別帳戶,並使用其他參數來設定或變更特定屬性。 以下是最常見的參數清單:
-Department 「 <department name> 」
-DisplayName 「 <full user name> 」
-FacsimilieTelephoneNumber 「 <fax number> 」
-GivenName 「 <user first name> 」
-Surname 「 <user last name> 」
-Mobile 「 <mobile phone number> 」
-JobTitle 「 <job title> 」
-PreferredLanguage 「 <language> 」
-StreetAddress 「 <street address> 」
-City 「 <city name> 」
-State 「 <state name> 」
-PostalCode 「 <postal code> 」
-Country 「 <country name> 」
-TelephoneNumber 「 <office phone number> 」
-UsageLocation 「 <2-character country or region code> 」
這是 ISO 3166-1 Alpha-2 (A2) 兩個字母的國家或地區代碼。
如需其他參數,請參閱 Set-AzureADUser。
注意
您必須先指派使用位置,才能將授權指派給使用者帳戶。
若要顯示使用者帳戶的使用者主體名稱,請執行下列命令。
Get-AzureADUser | Sort UserPrincipalName | Select UserPrincipalName | More
此命令會指示 PowerShell:
取得 Get-AzureADUser) (使用者帳戶的所有資訊,並將其傳送至下一個命令 (|) 。
依字母順序排序使用者主體名稱清單 (排序 UserPrincipalName) ,並將它傳送至下一個命令 (|) 。
只顯示每個帳戶的使用者主體名稱屬性 (選取 UserPrincipalName) 。
在 [ 更多) ] (一次顯示一個畫面。
若要根據帳戶的顯示名稱來顯示帳戶的使用者主體名稱 (名字和姓氏) ,請執行下列命令。 填入 $userName 變數,並移除 < and > 字元:
$userName="<Display name>"
Write-Host (Get-AzureADUser | where {$_.DisplayName -eq $userName}).UserPrincipalName
本範例會顯示顯示名稱 為 Caleb Sills 之使用者帳戶的使用者主體名稱。
$userName="Caleb Sills"
Write-Host (Get-AzureADUser | where {$_.DisplayName -eq $userName}).UserPrincipalName
藉由使用 $upn 變數,您可以根據個別帳戶的顯示名稱進行變更。 以下是將 Belinda Newman 的使用位置設定為法國的範例。 但它會指定她的顯示名稱,而不是她的使用者主體名稱:
$userName="Belinda Newman"
$upn=(Get-AzureADUser | where {$_.DisplayName -eq $userName}).UserPrincipalName
Set-AzureADUser -ObjectID $upn -UsageLocation "FR"
變更所有使用者帳戶的屬性
若要變更所有使用者的屬性,您可以使用 Get-AzureADUser 和 Set-AzureADUser Cmdlet 的組合。 下列範例會將所有使用者的使用位置變更為 法國:
Get-AzureADUser | Set-AzureADUser -UsageLocation "FR"
此命令會指示 PowerShell:
(Get-AzureADUser) 取得使用者帳戶的所有資訊,並將其傳送至下一個命令 (|) 。
將使用者位置設定為法國 (Set-AzureADUser -UsageLocation 「FR」) 。
變更一組特定使用者帳戶的屬性
若要變更特定使用者帳戶集的屬性,您可以使用 Get-AzureADUser、 Where 和 Set-AzureADUser Cmdlet 的組合。 下列範例會將會計部門中所有使用者的使用位置變更為 法國:
Get-AzureADUser | Where {$_.Department -eq "Accounting"} | Set-AzureADUser -UsageLocation "FR"
此命令會指示 PowerShell:
(Get-AzureADUser) 取得使用者帳戶的所有資訊,並將其傳送至下一個命令 (|) 。
尋找其 Department 屬性設定為 「Accounting」 的所有使用者帳戶 (Where {$_。Department -eq 「Accounting」}) ,並將產生的資訊傳送至下一個命令 (|) 。
將使用者位置設定為法國 (Set-AzureADUser -UsageLocation 「FR」) 。
使用適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組。
若要使用 Microsoft Azure Active Directory Module for Windows PowerShell 來設定使用者帳戶的屬性,請使用 Set-MsolUser Cmdlet,並指定要設定或變更的屬性。
注意
PowerShell Core 不支援適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組和名稱有 Msol 的 Cmdlet。從 PowerShell 執行這些 Cmdlet。
變更特定使用者帳戶的屬性
若要設定特定使用者帳戶的屬性,請使用 Set-MsolUser Cmdlet 並指定要設定或變更的屬性。
您可以使用 -UserPrincipalName 參數來識別帳戶,並使用其他參數來設定或變更特定屬性。 以下是最常見的參數清單。
-City 「 <city name> 」
-Country 「 <country name> 」
-Department 「 <department name> 」
-DisplayName 「 <full user name> 」
-Fax 「 <fax number> 」
-FirstName 「 <user first name> 」
-LastName 「 <user last name> 」
-MobilePhone 「 <mobile phone number> 」
-Office 「 <office location> 」
-PhoneNumber 「 <office phone number> 」
-PostalCode 「 <postal code> 」
-PreferredLanguage 「 <language> 」
-State 「 <state name> 」
-StreetAddress 「 <street address> 」
-Title 「 <title name> 」
-UsageLocation 「 <2-character country or region code> 」
這是 ISO 3166-1 Alpha-2 (A2) 兩個字母的國家或地區代碼。
如需其他參數,請參閱 Set-MsolUser。
若要查看所有使用者的使用者主體名稱,請執行下列命令:
Get-MSolUser | Sort UserPrincipalName | Select UserPrincipalName | More
此命令會指示 PowerShell:
取得 Get-MsolUser) (使用者帳戶的所有資訊,並將其傳送至下一個命令 (|) 。
依字母順序排序使用者主體名稱清單 (排序 UserPrincipalName) ,並將它傳送至下一個命令 (|) 。
只顯示每個帳戶的使用者主體名稱屬性 (選取 UserPrincipalName) 。
在 [ 更多) ] (一次顯示一個畫面。
若要根據帳戶的顯示名稱來顯示帳戶的使用者主體名稱 (名字和姓氏) ,請執行下列命令。 填入 $userName 變數,然後移除 < and > 字元。
$userName="<Display name>"
Write-Host (Get-MsolUser | where {$_.DisplayName -eq $userName}).UserPrincipalName
此範例會顯示名為 Caleb Sills 之使用者的使用者主體名稱:
$userName="Caleb Sills"
Write-Host (Get-MsolUser | where {$_.DisplayName -eq $userName}).UserPrincipalName
藉由使用 $upn 變數,您可以根據個別帳戶的顯示名稱進行變更。 以下範例會將 Belinda Newman 的使用位置設定為 法國,但會指定其顯示名稱,而不是使用者主體名稱:
$userName="<display name>"
$upn=(Get-MsolUser | where {$_.DisplayName -eq $userName}).UserPrincipalName
Set-MsolUser -UserPrincipalName $upn -UsageLocation "FR"
變更所有使用者帳戶的屬性
若要變更所有使用者的屬性,請使用 Get-MsolUser 和 Set-MsolUser Cmdlet 的組合。 下列範例會將所有使用者的使用位置變更為 法國:
Get-MsolUser | Set-MsolUser -UsageLocation "FR"
此命令會指示 PowerShell:
(Get-MsolUser) 取得使用者帳戶的所有資訊,並將其傳送至下一個命令 (|) 。
將使用者位置設定為法國 (Set-MsolUser -UsageLocation 「FR」) 。
變更一組特定使用者帳戶的屬性
若要變更特定使用者帳戶集的屬性,您可以使用 Get-MsolUser、 Where 和 Set-MsolUser Cmdlet 的組合。 下列範例會將會計部門中所有使用者的使用位置變更為 法國:
Get-MsolUser | Where {$_.Department -eq "Accounting"} | Set-MsolUser -UsageLocation "FR"
此命令會指示 PowerShell:
(Get-MsolUser) 取得使用者帳戶的所有資訊,並將其傳送至下一個命令 (|) 。
尋找其 Department 屬性設定為 「Accounting」 的所有使用者帳戶 (Where {$_。Department -eq 「Accounting」}) ,並將產生的資訊傳送至下一個命令 (|) 。
將使用者位置設定為法國 (Set-MsolUser -UsageLocation 「FR」) 。
另請參閱
以 PowerShell 管理 Microsoft 365 使用者帳戶、授權和群組