0

I have written a powershell script but am getting an the following Error: The term 'Get-MsolUserLicense' is not recognized as the name of a cmdlet

The module MSOnline is installed.

 #Script to change users password, inbox to shared mailbox and remove any active licenses

if (Get-Module -ListAvailable -Name ExchangeOnlineManagement) {
    Write-Host " Exchange Module exists"

} 
else {
    Write-Host "Exchange Module does not exist. Installing now"
    Install-Module -Name ExchangeOnlineManagement
}
if (Get-Module -ListAvailable -Name MSOnline) {
    Write-Host "MS Online Module exists"

} 
else {
    Write-Host "Exchange Module does not exist. Installing now"
    Install-Module -Name MSOnline
}
$User = Read-Host -Prompt 'Input the user name'
$password = Read-Host -Prompt 'Enter random password'
Try {
    
    Connect-MsolService
    Connect-ExchangeOnline
    Set-MsolUserPassword -UserPrincipalName $User -NewPassword $password
    Get-DistributionGroup | Where-Object { (Get-DistributionGroupMember $_).PrimarySmtpAddress -contains $userEmail } | ForEach-Object { Remove-DistributionGroupMember -Identity $_.Name -Member $userEmail -Confirm:$false }
    Set-Mailbox -Identity $User -Type Shared

    # Remove all licenses from the user
    $licenses = Get-MsolUserLicense -UserPrincipalName $User
    Set-MsolUserLicense -UserPrincipalName $User -RemoveLicenses $licenses.AccountSkuId
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
} 

Is there another way using PS that I can remove a users 365 licence?

Shaggy89
  • 689
  • 1
  • 5
  • 18
  • I think you want `$licenses = (Get-MSOLUser -UserPrincipalName $User).Licenses`, but you realy should ask for the UPN here: `$User = Read-Host -Prompt 'Input the user's UserPrincipalName'` – Theo Apr 01 '23 at 11:08
  • Hi @Theo out of interest why would that make a difference? I'm trying to streamline an offboarding process so if I can get the users principle name from the email I can change it to your line above. – Shaggy89 Apr 01 '23 at 22:40
  • Because as the error explains, there is no function `Get-MsolUserLicense`.. To get the list of licenses for a user you must get the user object with `Get-MSOLUser` and take the licenses from its `.Licenses` property – Theo Apr 02 '23 at 09:45

1 Answers1

0

Microsoft retired the Azure AD Graph and MSOnline PowerShell licensing assignment APIs and PowerShell cmdlets on March 31, 2023. Link

You must now use the Microsoft Graph PowerShell module and Set-MgUserLicense cmdlet.

jfrmilner
  • 1,458
  • 10
  • 11