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

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

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

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

オンプレミスの Active Directory ドメイン サービスから同期されたアカウントには、既定では場所が指定されません。 これらのアカウントの場所は、以下から構成できます。

  • 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 コマンドを実行して、使用可能なライセンス プランと、組織内の各プランで使用可能なライセンスの数を表示します。 各プランで利用可能なライセンスの数は、 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 アルファ-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 (カスタマー ロックボックス) サービスがオフになっている 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 @()

別のユーザーからライセンスの割り当てをコピーして、ユーザーにライセンスを割り当てる

次の例では、belindan@litwareinc.comに適用されているのと同じライセンス プランをjamesp@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 および subscribedSku API