PowerShell を使用して Microsoft 365 ライセンスをユーザー アカウントに割り当てる

この記事は、Microsoft 365 Enterprise および Office 365 Enterprise の両方に適用されます。

ユーザーは、アカウントにライセンス プランからライセンスが割り当てられるまで、Microsoft 365 サービスを使用できません。 PowerShell を使用して、ライセンスのないアカウントにライセンスをすばやく割り当てることができます。

ユーザー アカウントには、まず場所を割り当てる必要があります。 場所の指定は、Microsoft 365 管理センターで新しいユーザー アカウントを作成する際に必要な部分です

オンプレミスの Active Directory Domain Servicesから同期されたアカウントには、既定では場所が指定されていません。 これらのアカウントの場所は、次の場所から構成できます。

  • Microsoft 365 管理センター
  • PowerShell
  • Azure portal (Active Directory>ユーザー ユーザー> アカウント >プロファイル>連絡先情報>国または地域)。

注:

Microsoft 365 管理センターを使用してユーザー アカウントにライセンスを割り当てる方法について説明します。 その他のリソースの一覧については、「 ユーザーとグループの管理」を参照してください。

Microsoft Graph PowerShell SDK を使用してユーザー アカウントに Microsoft 365 ライセンスを割り当てる

注:

次のスクリプトでは、Microsoft Graph Powershell を使用します。 詳細については、「 Microsoft Graph PowerShell の概要」を参照してください。

無人スクリプトでさまざまな方法を使用して認証 Connect-Graph する方法については、 Microsoft Graph PowerShell の認証モジュール コマンドレットに関する記事を参照してください。

まず、Microsoft 365 テナントに接続します

ユーザーのライセンスの割り当てと削除には、User.ReadWrite.All アクセス許可スコープ、または 「ライセンスの割り当て」 Microsoft Graph API リファレンス ページに記載されている他のアクセス許可のいずれかが必要です。

テナントで使用できるライセンスを読み取るために、Organization.Read.All アクセス許可スコープが必要です。

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

コマンドをGet-MgSubscribedSku実行して、使用可能なライセンス プランと、organization内の各プランで使用可能なライセンスの数を表示します。 各プランで利用可能なライセンスの数は、 ActiveUnits - WarningUnits - ConsumedUnits です。 ライセンス プラン、ライセンス、およびサービスの詳細については、「 PowerShell でライセンスとサービスを表示する」を参照してください。

organizationでライセンスのないアカウントを見つけるには、次のコマンドを実行します。

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

organizationでライセンスのない同期ユーザーを見つけるには、次のコマンドを実行します。

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

-All パラメーターを使用せずに Get-MgUser コマンドレットを使用すると、最初の 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) サービスと LOCKBOX_ENTERPRISE (Customer Lockbox) サービスがオフになっているSPE_E5 (Microsoft 365 E5) を割り当てます。

$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と Forms サービス プランをオフにします。

$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と Forms サービス プランをオフにします。

$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 ユーザーを使用 する: assignLicense API と subscribedSku API