Invoke-MgmtGraphUserLicenseAction
MgmtGraph: Manages license assignments for a Microsoft Graph user
#Requires -Version 5.1
#Requires -Modules Microsoft.Graph.Users
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$Identity,
[string[]]$AddLicense,
[string[]]$RemoveLicense
)
Process {
try {
$addList = @()
foreach ($sku in $AddLicense) {
$addList += @{ SkuId = $sku }
}
$params = @{
'UserId' = $Identity
'AddLicenses' = $addList
'RemoveLicenses' = $RemoveLicense
'ErrorAction' = 'Stop'
}
$user = Set-MgUserLicense @params
$result = [PSCustomObject]@{
Identity = $Identity
AddedCount = ($AddLicense | Measure-Object).Count
RemovedCount = ($RemoveLicense | Measure-Object).Count
Action = "LicenseActionExecuted"
Status = "Success"
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
Write-Output $result
}
catch {
throw
}
}Specifies the UserPrincipalName or ID of the user to manage.
Optional. Specifies an array of SKU IDs to assign to the user.
Optional. Specifies an array of SKU IDs to remove from the user.