Windows PowerShell Excels at Carrying Out Bulk Operations

 

Topic Last Modified: 2014-05-27

Historically, graphical user interfaces like the Office 365 Admin center have been at their best when you have a one-off operation to perform. Need to disable one user account? It’s usually way faster, way easier, and way less error-prone to simply clear a checkbox than it is to open up Windows PowerShell and type a command that does the exact same thing. You say you need to change a user’s phone number and you’re bound and determined to use the Admin center to do that? We don’t blame you; that’s probably how we’d do it, too.

But that might not be the way we would do things if we had to change thousands of phone numbers, or if we had to perform some other operation in bulk. For example, suppose your boss comes up to you and says, “Hey, we need to remove Ken Myer from all our SharePoint Online sites. How quickly can you do that?”

Well, let’s see. You have several hundred SharePoint Online sites, and you don’t know even know which ones Ken is a member of. That means you’ll have to start at the SharePoint Online Admin center and:

  1. Click the URL of the first site.

  2. In the site collection properties box, click the Web Site Address link to open the site.

  3. On the site, click Share.

  4. In the Share dialog box click the link that shows you all the users who have permissions to the site:

    Viewing the members of a SharePoint Online site.
  5. In the Shared With dialog box, click Advanced.

  6. Scroll down the list of users, find and select Ken Myer (assuming he has permissions to the site), and then click Remove User Permissions.

  7. Repeat for all your other sites.

How long will it take you to do this? Rough estimate: forever.

Unless, of course, you’ve installed the SharePoint Online Windows PowerShell module. In that case, you can run this one little command to remove Ken Myer from all your sites:

Get-SPOSite | ForEach-Object {Remove-SPOUser -Site $_.Url -LoginName "kenmyer@litwareinc.com"}

We should mention that, if you try this, you will get an error message if you happen to hit a site that Ken doesn’t currently have permissions to. (There are ways to avoid that error message, but we aren’t going to deal with them in this article.) However, the command will continue to run and, when it completes, Ken Myer will no longer have permissions to any of your sites. And, best of all, the whole thing might take a minute or two to complete.

Oh, and that’s a minute or two total, not a minute or two per site.

And what if you decide to later reinstate Ken as a member on all your sites? No problem:

Get-SPOSite | ForEach-Object {Add-SPOUser -Site $_.Url -LoginName "kenmyer@litwareinc.com" -Group "Members"}

Is that cool or what?

NoteNote:
Keep in mind that we “cheated” a little but in our previous example: we assumed that you wanted to add Ken back to each of your SharePoint Online sites. That makes for a good example of the cool things you can do using Windows PowerShell, but it might not reflect your real-life needs: in real life, you might want to add Ken back to just some of your sites, the sites he was previously a member of. Can you do that? Yes, you can, although we won’t explain how in this introductory overview. Just note that, before you remove Ken’s membership privileges, you can use Windows PowerShell to record that information in a text file. When the time comes to restore those privileges, you can then add Ken only to the sites specified in that file. For more information, see Creating sites and adding users using Windows PowerShell.


Next: Windows PowerShell is Great at Filtering Data

 
Show: