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?