One of the most common bulk administration tasks, after reporting, is updating objects. Let’s say we needed to assign licenses to this group of users, but they didn’t have their usage location set. In order to assign the proper license plans and service plans, you’ll need to configure the correct location.
Using the MSOnline cmdlets, we could retrieve our list of users and then, through piping, use the output of the Get-MsolUser cmdlet as the input for the Set-MsolUser cmdlet:
Get-Msoluser -MaxResults 10 -Department “Project Management” | Set-MsolUser -UsageLocation US
Figure 2.40 – Updating the UsageLocation with Set-MsolUser
The Azure AD cmdlets also support piping input:
Get-AzureADUser -Top 10 -Filter “Department eq ‘Project Management'” | Set-AzureADUser -UsageLocation US
Figure 2.41 – Updating the UsageLocation with Set-AzureADUser
Finally, you can also use the Set-MgUser cmdlet to update user objects with the Microsoft Graph PowerShell, though the pipeline syntax is a little different still. In this example, the piped data is processed using a Foreach command, instructing PowerShell to loop through the list of users returned, substituting the actual individual object (represented by $_) and the property of the object to be retrieved (represented by the .id):
Get-MgUser -Filter “Department eq ‘Project Management'” -Top 5
-ConsistencyLevel Eventual -Property * | Foreach { Update-MgUser -UserId $_.id -UsageLocation US }
Figure 2.42 – Updating the UsageLocation with Update-MgUser
Updating Licenses
License management is also a task that is often performed via scripting.
In order to determine the licensing plan to assign with the MSOnline module, you’ll need to retrieve the list of valid products using the Get-MsolAccountSku cmdlet. After that, you can assign a license using the tenant:LICENSINGPLAN syntax:
Get-MsolAccountSku
Set-MsolUserLicense -UserPrincipalName [email protected] -AddLicenses “M365w520429:TEAMS_EXPLORATORY”
Figure 2.43 – Adding a license to a user with the MSOnline module
To assign a license with the Azure AD PowerShell is a bit more complicated, as it involves creating a special licensing object. In this example, you’ll assign the user Aamir the TEAMS_EXPLORATORY license:
Get-AzureADSubscribedSku
$TeamsSku = Get-AzureADSubscribedSku | ? { $_.SkuPartNumber -eq “TEAMS_EXPLORATORY” }
$License = New-Object -TypeName Microsoft.Open.AzureAD.Model.
AssignedLicense
$License.SkuId = $TeamsSku.SkuId
$LicenseToAssign = New-Object -TypeName Microsoft.Open.AzureAD.Model. AssignedLicenses
$LicenseToAssign.AddLicenses = $License
Set-AzureADUserLicense -ObjectId [email protected] -AssignedLicenses $LicenseToAssign
Figure 2.44 – Adding a license with the Set-AzureADUserLicense cmdlet
In the final license example, you’ll use the Microsoft Graph PowerShell cmdlets. It works similarly to the Azure AD cmdlet, but the syntax requires a hash table to hold the license SkuId property:
$user = Get-Mguser -UserId [email protected] -Property *
$TeamsSku = Get-MgSubscribedSku -all | Where SkuPartNumber -eq “TEAMS_ EXPLORATORY”
Set-MgUserLicense -UserId $user.Id -AddLicenses @{SkuId = $TeamsSku. SkuId} -RemoveLicenses @()
Figure 2.45 – Adding a license with the Set-MgUserLicense cmdlet
Depending on your scenario, managing licensing through one of the PowerShell interfaces may be the most efficient way to craft custom license configurations.
Further Reading
Managing licenses can be a complex topic, especially when considering options for enabling or disabling individual service plans within a license or replacing licensing options for users. You can see more in-depth information regarding different capabilities of PowerShell-based licensing at https://learn.microsoft.com/en-us/microsoft-365/enterprise/ view-licenses-and-services-with-microsoft-365-powershell.