使用 PowerShell 將 Microsoft 365 授權指派給用戶帳戶

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

使用者必須先從授權方案指派授權,才能使用任何 Microsoft 365 服務。 您可以使用 PowerShell 快速將授權指派給未授權的帳戶。

用戶帳戶必須先指派位置。 指定位置是在 Microsoft 365 系統管理中心 中建立新用戶帳戶的必要部分。

從您的 內部部署的 Active Directory Domain Services 同步處理的帳戶預設不會指定位置。 您可以從下列位置設定這些帳戶的位置:

  • Microsoft 365 系統管理中心
  • PowerShell
  • Azure 入口網站 (Active Directory>使用者>用戶帳戶>配置檔>連絡資訊>國家或地區) 。

使用 Microsoft Graph PowerShell SDK 將 Microsoft 365 授權指派給使用者帳戶

注意事項

下列腳本使用 Microsoft Graph Powershell。 如需詳細資訊,請參閱 Microsoft Graph PowerShell 概觀

如需如何在自動腳本中使用不同方法進行驗證 Connect-Graph 的資訊,請參閱 Microsoft Graph PowerShell 中的驗證模組 Cmdlet 一文。

首先, 聯機到您的 Microsoft 365 租使用者

指派和移除用戶的授權需要 User.ReadWrite.All 許可權範圍或 [指派授權] Microsoft 圖形 API 參考頁面中所列的其他許可權之一。

需要 Organization.Read.All 許可權範圍,才能讀取租用戶中可用的授權。

Connect-MgGraph -Scopes User.ReadWrite.All, Organization.Read.All

Get-MgSubscribedSku執行 命令以檢視組織中每個方案中可用的授權方案和可用授權數目。 每個方案中的可用授權數目是 ActiveUnits - WarningUnits - ConsumedUnits。 如需授權方案、授權和服務的詳細資訊,請參 閱使用PowerShell檢視授權和服務

若要在您的組織中尋找未授權的帳戶,請執行此命令。

Get-MgUser -Filter 'assignedLicenses/$count eq 0' -ConsistencyLevel eventual -CountVariable unlicensedUserCount -All

若要尋找您組織中未授權的同步使用者,請執行此命令。

Get-MgUser -Filter 'assignedLicenses/$count eq 0 and OnPremisesSyncEnabled eq true' -ConsistencyLevel eventual -CountVariable unlicensedUserCount -All -Select UserPrincipalName

您只能將授權指派給已將 UsageLocation 屬性設定為有效 ISO 3166-1 alpha-2 國家/地區代碼的用戶帳戶。 例如,US 代表美國、FR 代表法國。 某些 Microsoft 365 服務不適用於特定國家/地區。 如需詳細資訊,請 參閱關於授權限制

若要尋找沒有 UsageLocation 值的帳戶,請執行此命令。

Get-MgUser -Select Id,DisplayName,Mail,UserPrincipalName,UsageLocation,UserType | where { $_.UsageLocation -eq $null -and $_.UserType -eq 'Member' }

若要在帳戶上設定 UsageLocation 值,請執行此命令。

$userUPN="<user sign-in name (UPN)>"
$userLoc="<ISO 3166-1 alpha-2 country code>"

Update-MgUser -UserId $userUPN -UsageLocation $userLoc

例如:

Update-MgUser -UserId "belindan@litwareinc.com" -UsageLocation US

如果您使用 Get-MgUser Cmdlet 而不使用 -All 參數,則只會傳回前 100 個帳戶。

將授權指派給用戶帳戶

若要將授權指派給使用者,請在PowerShell中使用下列命令。

Set-MgUserLicense -UserId $userUPN -AddLicenses @{SkuId = "<SkuId>"} -RemoveLicenses @()

此範例會將SPE_E5 (Microsoft 365 E5) 授權方案中的授權指派給未授權的使用者belindan@litwareinc.com

$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'
Set-MgUserLicense -UserId "belindan@litwareinc.com" -AddLicenses @{SkuId = $e5Sku.SkuId} -RemoveLicenses @()

此範例會將SPE_E5 (Microsoft 365 E5) 和 EMSPREMIUM (ENTERPRISE MOBILITY + SECURITY E5) 指派給使用者belindan@litwareinc.com

$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'
$e5EmsSku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'EMSPREMIUM'
$addLicenses = @(
    @{SkuId = $e5Sku.SkuId},
    @{SkuId = $e5EmsSku.SkuId}
)

Set-MgUserLicense -UserId "belinda@litwareinc.com" -AddLicenses $addLicenses -RemoveLicenses @()

此範例會使用 MICROSOFTBOOKINGS (Microsoft Bookings) 指派SPE_E5 (Microsoft 365 E5) ,並關閉LOCKBOX_ENTERPRISE (客戶加密箱) 服務:

$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'
$disabledPlans = $e5Sku.ServicePlans | `
    Where ServicePlanName -in ("LOCKBOX_ENTERPRISE", "MICROSOFTBOOKINGS") | `
    Select -ExpandProperty ServicePlanId

$addLicenses = @(
    @{
        SkuId = $e5Sku.SkuId
        DisabledPlans = $disabledPlans
    }
)

Set-MgUserLicense -UserId "belinda@litwareinc.com" -AddLicenses $addLicenses -RemoveLicenses @()

此範例會使用SPE_E5 (Microsoft 365 E5) 來更新使用者,並關閉 Sway 和窗體服務方案,同時讓使用者的現有停用方案保持在目前的狀態:

$userLicense = Get-MgUserLicenseDetail -UserId "belinda@litwareinc.com"
$userDisabledPlans = $userLicense.ServicePlans | `
    Where ProvisioningStatus -eq "Disabled" | `
    Select -ExpandProperty ServicePlanId

$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'
$newDisabledPlans = $e5Sku.ServicePlans | `
    Where ServicePlanName -in ("SWAY", "FORMS_PLAN_E5") | `
    Select -ExpandProperty ServicePlanId

$disabledPlans = ($userDisabledPlans + $newDisabledPlans) | Select -Unique

$addLicenses = @(
    @{
        SkuId = $e5Sku.SkuId
        DisabledPlans = $disabledPlans
    }
)

Set-MgUserLicense -UserId "belinda@litwareinc.com" -AddLicenses $addLicenses -RemoveLicenses @()

此範例會使用SPE_E5 (Microsoft 365 E5) 更新使用者,並關閉 Sway 和表單服務方案,同時讓使用者的現有停用方案保持在所有其他訂用帳戶的目前狀態:

$userLicense = Get-MgUserLicenseDetail -UserId belinda@litwareinc.com

$userDisabledPlans = $userLicense.ServicePlans | Where-Object ProvisioningStatus -eq "Disabled" | Select -ExpandProperty ServicePlanId

$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'

$newDisabledPlans = $e5Sku.ServicePlans | Where ServicePlanName -in ("SWAY", "FORMS_PLAN_E5") | Select -ExpandProperty ServicePlanId

$disabledPlans = ($userDisabledPlans + $newDisabledPlans) | Select -Unique

$result=@()
$allPlans = $e5Sku.ServicePlans | Select -ExpandProperty ServicePlanId

foreach($disabledPlan in $disabledPlans)
{
    foreach($allPlan in $allPlans)
    {
        if($disabledPlan -eq $allPlan)
        {
            $property = @{
                Disabled = $disabledPlan
            }
        }
     }
     $result += New-Object psobject -Property $property
}


$finalDisabled = $result | Select-Object -ExpandProperty Disabled


$addLicenses = @(
    @{
        SkuId = $e5Sku.SkuId
        DisabledPlans = $finalDisabled
    }
)


Set-MgUserLicense -UserId belinda@litwareinc.com -AddLicenses $addLicenses -RemoveLicenses @()

藉由從另一個使用者複製授權指派,將授權指派給使用者

此範例會 jamesp@litwareinc.com 使用已套用至 的相同授權方案來 belindan@litwareinc.com指派 :

$mgUser = Get-MgUser -UserId "belindan@litwareinc.com" -Property AssignedLicenses
Set-MgUserLicense -UserId "jamesp@litwareinc.com" -AddLicenses $mgUser.AssignedLicenses -RemoveLicenses @()

將使用者移至不同的訂用帳戶 (授權方案)

此範例會將使用者從SPE_E3 (Microsoft 365 E3) 授權方案升級至SPE_E5 (Microsoft 365 E5) 授權方案:

$e3Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E3'
$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'

# Unassign E3
Set-MgUserLicense -UserId "belindan@litwareinc.com" -AddLicenses @{} -RemoveLicenses @($e3Sku.SkuId)
# Assign E5
Set-MgUserLicense -UserId "belindan@litwareinc.com" -AddLicenses @{SkuId = $e5Sku.SkuId} -RemoveLicenses @()

您可以使用此命令來驗證使用者帳戶的訂用帳戶變更。

Get-MgUserLicenseDetail -UserId "belindan@litwareinc.com"

另請參閱

使用 PowerShell 管理 Microsoft 365

使用 PowerShell 管理 Microsoft 365

開始使用 Microsoft Graph PowerShell SDK

使用 Microsoft Graph 使用者:assignLicensesubscribedSku API