The below script can be used in off-boarding below tasks for a resigned employees as a bulk operation.
This script will help in below actions for Exchange online and AD tasks to be removed in a Exchange hybrid environment:
- Convert exchange online mailbox to shared Mailbox.
- Disable the Mailbox protocols – OWA,ActiveSync, POP, IMAP, MAPI & OWA for devices.
- Hide the user from GAL.
- Remove the user from respective licenses E3,E5,EMS E3 & EMS E5 Licenses.
- Cancel all the calendar future meetings.
- Remove the user account from all groups.
- Set the account expiry of the AD account.
- Remove the IP Phone Attribute.
- Remove the manager field.
- Set out of office.
Prerequisites:
1.Run this from a management server where it has Exchange, Active Directory, MSonline and exchange online MFA PowerShell modules installed on it.
2.This will run from MFA enabled Admin accounts from windows powershell,connect to exchange online and msonline. Make sure to run this script from an elevated windows powershell mode.
3.Change the csv file location to your location
Connect-EXOPSSession -UserPrincipalname adminid@domain.com – Change the admin userprincipalname to your admin id.
Export-csv “c:\ops\Output\disabledusers.csv” – mention the location of the csv file
4. Create a CSV file which has only the userprincipalname of the resigned employees.
5. Change the OOF message details with the required information.
$MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse).FullName | Select-Object -Last 1) . "$MFAExchangeModule" $cred= Get-Credential Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn Import-Module activedirectory Connect-MsolService -Credential $cred Connect-EXOPSSession -UserPrincipalname adminid@domain.com $E3 = "tenantname:ENTERPRISEPREMIUM" $E5 = "tenantname:ENTERPRISEPACK" $EMSE3 = "tenantname:EMSPREMIUM" $EMSE5= "tenantname:EMS" Import-csv "mention the CSV path location" | foreach { $UPN = $_.userPrincipalName #Convert to shared mailbox Set-Mailbox $UPN -Type “Shared” #Disable the Mailbox protocols Set-CASMailbox -identity $upn -OWAEnabled:$false -ImapEnabled:$false -MAPIEnabled:$false -PopEnabled:$false -ActiveSyncEnabled:$false -OWAforDevicesEnabled:$false -Confirm:$false -verbose #Cancel all the future meetings Remove-Calendarevents -identity $UPN.userprincipalname -CancelOrganizedMeetings -Confirm:$False #Remove the license $msolupn= Get-Msoluser -Userprincipalname $UPN | select Objectid,Userprincipalname,Licenses Set-MsolUserLicense -UserPrincipalName $UPN.UserPrincipalName -RemoveLicenses $E3 -ErrorAction SilentlyContinue Set-MsolUserLicense -UserPrincipalName $UPN.UserPrincipalName -RemoveLicenses $E5 -ErrorAction SilentlyContinue Set-MsolUserLicense -UserPrincipalName $UPN.UserPrincipalName -RemoveLicenses $EMSE3 -ErrorAction SilentlyContinue Set-MsolUserLicense -UserPrincipalName $UPN.UserPrincipalName -RemoveLicenses $EMSE5 -ErrorAction SilentlyContinue #Hide from GAL Set-RemoteMailbox -identity $upn -HiddenFromAddressListsEnabled:$True #Set the OOF Set-MailboxAutoReplyConfiguration -Identity $UPN -AutoReplyState Enabled -ExternalMessage "“Please note that i no longer work for ezcloudinfo anymore.Kindly contact HR department via hr@ezcloudinfo.com for further communication.“" #Remove from Distribution Lists Get-ADUser -Identity $UPN -Properties MemberOf | ForEach-Object { $_.MemberOf | Remove-ADGroupMember -Members $_.DistinguishedName -Confirm:$false #Remove the manager field Set-Aduser -Identity $UPN -Manager $null #Remove IP Phone attribute Set-ADuser -Identity $UPN -Clear ipPhone #Set the Account Expiry Set-ADAccountExpiration -Identity $UPN -TimeSpan 0.0:30 Write-Host The Users have been offboarded successfully -ForegroundColor Green Get-Mailbox $UPN | select-Object name,recipienttypedetails | Export-csv "c:\ops\Output\disabledusers.csv" -NoTypeInformation -Force -Append } }
Thanks & Regards
Sathish Veerapandian
thank you very much for the information
LikeLike