Again to share some of their own small script to write Ah, sometimes need to use their own O365 account to do some testing, in order to make the test environment more realistic, more close to the production environment, many times I will use the script to create some users, and then generate some mail, calendar or other data, After testing, these users and data are to be disposed of, if you do not use custom domain users and groups, etc. are deleted, then the domain name can not be removed, the manual deletion of these data is certainly possible, but for the sake of convenience, in fact, you can use some scripts to directly O365 users and groups to clear all out. Write your own simple script can be completed, of course, for the global admin, this certainly can not be arbitrarily deleted
The following is the contents of the script that requires the MS Online Module to be installed before running
try{$Error. Clear () write-host "$ (get-date) * deprovision users:" $Users = get-msoluser -all -erroraction stop$users | %{$Role = Get-msoluserrole -userprincipalname $_. userprincipalname# Exclude global adminif (($Role -eq $null) -or ($Role. objectid - ne "62e90394-69f5-4237-9190-012177145e10")) {remove-msoluser -userprincipalname $_. userprincipalname -force -erroraction stopwrite-host "$ (get-date) * $ ($_. userPrincipalName) removed. "}} start-sleep 1# Remove user get-msoluser -returndeletedusers -erroraction stop | from Recycle Bin remove-msoluser -removefromrecyclebin -force -erroraction stopwrite-host "$ (Get-Date) * done. users already deleted "start-sleep 1write-host " $ (Get-Date) * deprovision groups. " Get-msolgroup -erroraction stop | remove-msolgroup -force -erroraction stopwrite-host "$ (get-date) * Done. groups already deleted "}catch{write-warning $Error [0]. Exception.Message}
The script is quite simple, basically can understand, for the experimental environment, is still more useful. Of course, if you have some contacts or other data in exchange online that you want to automatically erase, you also need to use the Exchange online module, which does not introduce
Using PowerShell deprovision O365 Resources