Running PowerShell against a large numbers of users in Office 365

Microsoft Office 365 is a beast of a product and there maybe times when you need to run a script or command against a bulk or all the users in it.  Typically, code snippets and examples from across the internet show simply to connect in PowerShell to Office 365 then simply run commands against. 

However, if you are like me, where your organisation has thousands of user accounts then running a command to simply list permissions in a calendar can result in the command taking hours and eventually timing out – not great!

There is a PowerShell module called ‘RobustCloudCommand’ that runs the commands you would normally run from your client in your Office 365 server environment, shortening the times to calculate results and keeping the connection open and live whilst these commands are being run.

Install the RobustCloudCommand module from Microsoft:

Install-Module -Name RobustCloudCommand

To run the module, connect to your Office 365 tenancy and use this as a wrapper to call your code.

For example, in my case to set ‘Default’ in every users calendar to ‘LimitedDetails’ run this:

$Users = invoke-command -scriptblock {Get-Mailbox -Resultsize Unlimited}

Start-RobustCloudCommand -recipients $Users -logfile C:\temp\out.log -ScriptBlock {Set-MailboxFolderPermission -Identity $input”:\calendar” -User Default -AccessRights LimitedDetails}

Two Microsoft articles worth reading to expand on the above:

https://techcommunity.microsoft.com/t5/exchange-team-blog/updated-running-powershell-cmdlets-for-large-numbers-of-users-in/ba-p/1000628

https://techcommunity.microsoft.com/t5/exchange-team-blog/running-powershell-cmdlets-for-large-numbers-of-users-in-office/ba-p/604280

I’ve reblogged this for my own sanity sake as it took some searching to find so will hopefully prove useful if you hit this page by accident.

No comments yet.

Leave a Reply