Skip to content

PowerShell for Microsoft 365 enables you to manage your Microsoft 365 settings from the command line.

Notifications You must be signed in to change notification settings

PaulNiklausPraveen/Microsoft-365-PowerShell

Repository files navigation

#This Script will install Msonline powershell module and remove license from deleted users

#Installs Microsoft Azure Active Directory Module for Windows PowerShell
Install-Module Msonline

#Imports the module
Import-Module Msonline

#Initiates a connection to Azure Active Directory
connect-Msolservice

$DeletedUsers=@()

$DeletedUSers=(Get-MsolUser –ReturnDeletedUsers |  Where-Object { $_.isLicensed -eq $True}).UserPrincipalName

Foreach($DeletedUser in $DeletedUsers)
{
Restore-MsolUser -UserPrincipalName $DeletedUser

Sleep 5

$License=(get-MsolUser -UserPrincipalName $DeletedUser).licenses.AccountSkuId

Write-Host "Removing the License : $License for $DeletedUser" -ForegroundColor Yellow

Set-MsolUserLicense -UserPrincipalName $DeletedUser -RemoveLicenses $License

Remove-MsolUser -UserPrincipalName $DeletedUser -Force
 
}

About

PowerShell for Microsoft 365 enables you to manage your Microsoft 365 settings from the command line.

Topics

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published