Using PowerShell deprovision O365 Resources

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.