Did you know, you can fully manage Office365 with PowerShell?
What? Wasn’t Office365 supposed to be in the cloud – how do I use powershell with THAT!? Is it black magic? No not really!
Office 365 uses the remote powershell functionality of Windows Powershell 2.0. It builds upon the Windows remote management service (you will find it in the services console of Vista+ or Windows2008+ by default, or installed as an addon on XP). It uses that to connect to a web service, and download the available commandlets – based on the logged in user. WHOAA!!
So, what can be managed using PowerShell in Office 365?
- You can manage domains
- Security Groups
- Enhanced Authorizations
- Identity Federation
- Partner support – where you designate a partner to manage your Office365
- And Exchange.
Note that Lync and SharePoint online are not manageable via powershell. So, still sound like black magic? What are the actual steps to use Powershell in Office365? Well here they are.
- Login to a machine, hopefully Windows 2008 (so it has Windows remote management service running).
- Start powershell
- Run the following command –
-
| PS C:\Users\Administrator> Set-ExecutionPolicy unrestricted Execution Policy Change The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution policy? [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): y PS C:\Users\Administrator> |
- Then, create a Office 365 credential using the following command –
-
| PS C:\Users\Administrator> $cred = Get-Credential |
- In the username/password window that opens – enter the credentials of an Office 365 account. You can also use your current identity – if federation has already been setup.
- Okay, next, start a new powershell session, say with the Microsoft Exchange Online -
-
| PS C:\Users\Administrator> $O365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook .com/powershell -Credential $cred -Authentication Basic -AllowRedirection WARNING: Your connection has been redirected to the following URI: "https://pod51008psh.outlook.com/PowerShell-LiveID?PSVersion=2.0 " PS C:\Users\Administrator> |
- Well hooray! Your session is started. Next, import all the commandlets, and print say the number of commands available, like this -
-
| PS C:\Users\Administrator> $importcmd = Import-PSSession $O365 PS C:\Users\Administrator> $importcmd.ExportedFunctions.Count 292 PS C:\Users\Administrator> |
- Well hooray! One more time! Now, lets run a command :)
-
| PS C:\Users\Administrator> Get-User Name RecipientType ---- ------------- DiscoverySearchMailbox {D919BA05-46A6-415f-80AD-7E09334B... UserMailbox Sahil UserMailbox sahiltest1 UserMailbox |
- And Sahil and sahiltest1, are my Office365 accounts right now :). w00T!