Set-EntraUserLicense

ユーザーに割り当てられたライセンスの一覧に、Microsoft オンライン サービスのライセンスを追加または削除します。

構文

Default (既定)

Set-EntraUserLicense

    -UserId <String>
    -AssignedLicenses <AssignedLicenses>
    [<CommonParameters>]

説明

Set-EntraUserLicenseは、Microsoft オンライン サービスのライセンスをユーザーに割り当てられたライセンスの一覧に追加または削除します。

委任されたシナリオの場合、呼び出し元のユーザーには、次のMicrosoft Entraロールのいずれかが少なくとも 1 つ必要です。

  • ディレクトリ製作者
  • ライセンス管理者
  • ユーザー管理者

: ライセンスを割り当てる前に、 Set-EntraUser -ObjectId user@contoso.com -UsageLocation '<two-letter-country-code e.g. GB/US>'を使用してユーザーに使用場所を割り当てます。

例 1: テンプレート ユーザーに基づいてユーザーにライセンスを追加する

Connect-Entra -Scopes 'User.ReadWrite.All'
$licensedUser = Get-EntraUser -UserId 'TemplateUser@contoso.com'
$targetUser = Get-EntraUser -UserId 'SawyerM@contoso.com'
$sourceUserLicenses = $licensedUser.AssignedLicenses
$licensesToAssign = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
foreach ($license in $sourceUserLicenses) {
    $assignedLicense = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
    $assignedLicense.SkuId = $license.SkuId
    $licensesToAssign.AddLicenses = $assignedLicense
    Set-EntraUserLicense -UserId $targetUser.Id -AssignedLicenses $licensesToAssign
}
Name                           Value
----                           -----
externalUserStateChangeDateTi…
businessPhones                 {8976546787}
postalCode                     444601
createdDateTime                06-11-2023 04:48:19
surname                        KTETSs
jobTitle                       Manager
employeeType
otherMails                     {SawyerM@contoso.com}
isResourceAccount
usageLocation                  DE
legalAgeGroupClassification    Adult
id                             cccccccc-2222-3333-4444-dddddddddddd
isLicenseReconciliationNeeded  False

この例では、テンプレート ユーザーに基づいてユーザーにライセンスを割り当てる方法を示します。

  • -UserId パラメーターは、ユーザーのオブジェクト ID を指定します (UserPrincipalName または ObjectId として)。
  • -AssignedLicenses パラメーターは、割り当てまたは削除するライセンスの一覧を指定します。

例 2: 別のユーザーからライセンスをコピーしてユーザーにライセンスを追加する

Connect-Entra -Scopes 'User.ReadWrite.All'
$licensedUser = Get-EntraUser -UserId 'AdeleV@contoso.com'
$user = Get-EntraUser -UserId 'SawyerM@contoso.com'
$license1 = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$license1.SkuId = $licensedUser.AssignedLicenses.SkuId[0]
$license2 = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$license2.SkuId = $licensedUser.AssignedLicenses.SkuId[1]
$addLicensesArray = @()
$addLicensesArray += $license1
$addLicensesArray += $license2
$licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$licenses.AddLicenses = $addLicensesArray
Set-EntraUserLicense -UserId $user.Id -AssignedLicenses $licenses
Name                           Value
----                           -----
externalUserStateChangeDateTi…
businessPhones                 {8976546787}
postalCode                     444601
createdDateTime                06-11-2023 04:48:19
surname                        KTETSs
jobTitle                       Manager
employeeType
otherMails                     {SawyerM@contoso.com}
isResourceAccount
usageLocation                  DE
legalAgeGroupClassification    Adult
id                             cccccccc-2222-3333-4444-dddddddddddd
isLicenseReconciliationNeeded  False

この例では、別のユーザーからライセンスをコピーして、ユーザーにライセンスを割り当てる方法を示します。

  • -UserId パラメーターは、ユーザーのオブジェクト ID を指定します (UserPrincipalName または ObjectId として)。
  • -AssignedLicenses パラメーターは、割り当てまたは削除するライセンスの一覧を指定します。

例 3: 割り当てられたユーザーのライセンスを削除する

Connect-Entra -Scopes 'User.ReadWrite.All'
$userPrincipalName = 'SawyerM@Mcontoso.com'
$user = Get-EntraUser -UserId $userPrincipalName
$skuId = (Get-EntraUserLicenseDetail -UserId $userPrincipalName).SkuId
$licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$licenses.RemoveLicenses = $skuId
Set-EntraUserLicense -UserId $user.Id -AssignedLicenses $licenses
Name                           Value
----                           -----
displayName                    SawyerM
id                             cccccccc-2222-3333-4444-dddddddddddd
jobTitle
surname                        M
mail
userPrincipalName              SawyerM@contoso.com
mobilePhone
preferredLanguage
@odata.context                 https://graph.microsoft.com/v1.0/$metadata#users/$entity
businessPhones                 {}
officeLocation
givenName                      Sawyer

この例では、ユーザーの詳細を取得してユーザーのライセンスを削除する方法を示します。

  • -UserId パラメーターは、ユーザーのオブジェクト ID を指定します (UserPrincipalName または ObjectId として)。
  • -AssignedLicenses パラメーターは、割り当てまたは削除するライセンスの一覧を指定します。

例 4: 複数のユーザーにライセンスを一括割り当てする

Connect-Entra -Scopes 'Organization.ReadWrite.All'
# Retrieve the SkuId for the desired license plans
$skuId1 = (Get-EntraSubscribedSku | Where-Object { $_.SkuPartNumber -eq 'AAD_PREMIUM_P2' }).SkuId
$skuId2 = (Get-EntraSubscribedSku | Where-Object { $_.SkuPartNumber -eq 'EMS' }).SkuId
# Define the user to whom the licenses will be assigned
$users = ('AljosaH@Contoso.com', 'PalameeC@Contoso.com')
# You can, alternatively, import users from a csv file. For this example, the CSV should have a column named 'user'
$users = Import-Csv -Path "C:\path\to\your\users.csv" | Select-Object -ExpandProperty user
# Create license assignment objects
$license1 = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$license1.SkuId = $skuId1
$license2 = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$license2.SkuId = $skuId2
$licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$licenses.AddLicenses = $license1, $license2
# Assign the licenses to each user
foreach ($user in $users$users) {
    Set-EntraUserLicense -UserId $user -AssignedLicenses $licenses
}

パラメーター

-AssignedLicenses

割り当てまたは削除するライセンスの一覧を指定します。

パラメーターのプロパティ

型:AssignedLicenses
規定値:None
ワイルドカードのサポート:False
DontShow:False

パラメーター セット

(All)
配置:Named
必須:True
パイプラインからの値:True
プロパティ名別のパイプラインからの値:True
残りの引数からの値:False

-UserId

Microsoft Entra ID内のユーザーの ID (UserPrincipalName または ObjectId として) を指定します。

パラメーターのプロパティ

型:System.String
規定値:None
ワイルドカードのサポート:False
DontShow:False
Aliases:オブジェクト識別子, UPN, アイデンティティ, ユーザープリンシパルネーム

パラメーター セット

(All)
配置:Named
必須:True
パイプラインからの値:True
プロパティ名別のパイプラインからの値:True
残りの引数からの値:False

CommonParameters

このコマンドレットでは、一般的なパラメーター -Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction、-WarningVariable の各パラメーターがサポートされています。 詳細については、about_CommonParametersを参照してください。