Powershell Script for user offboarding

80 Views Asked by At

How can I get the Powershell script to block the users sign-in and remove the licenses and convert the usermailbox to sharedmailbox and assign the sharedmailbox to someone else by using script in M365 by using input file?

I tried below script but its not removing the licenses of the users from Input csv file

# Define the path to the CSV file containing user email IDs
$inputFilePath = "C:\FolderName\FileName.csv" 

# Read the CSV file
$userList = Import-Csv -Path $inputFilePath

foreach ($user in $userList) {
    $userEmail = $user.Email

    # Block user sign-in
    Set-AzureADUser -ObjectId (Get-AzureADUser -Filter "UserPrincipalName eq '$userEmail'").ObjectId -AccountEnabled $false

    # Remove licenses for the user

    Set-AzureADUserLicense -ObjectId (Get-AzureADUser -ObjectId $userEmail).ObjectId -RemoveLicenses "DEVELOPERPACK_E5"

    # Convert mailbox to shared mailbox
        Set-Mailbox -Identity $userEmail -Type Shared 

    Add-MailboxPermission -Identity $userEmail -User $Delegates -AccessRights FullAccess

}

Its only for Sign-in Blocked and convert mailbox from Usermailbox to Sharedmailbox

But my requirement is to Sign-in block, Remove the licenses, Convert mailbox from usermailbox to Sharedmailbox and assign the sharedmailbox to someone else as delegates with Full permission on it.

1

There are 1 best solutions below

0
Evaldas Blauzdziunas On

For removing licenses in O365 which i assume you are trying to do i used command

    Set-MsolUserLicense -UserPrincipalName $Username -RemoveLicenses $License

You can connect via Connect-MsolService which you can pass user credentials into, worked for my user decomm script.

Also try to Get-* the user license to display it in console, currently i have no access to O365 env, but i remember sometimes the names of licenses were different than they were in web interfaces, that also prevented from removing them, just a heads up.