In today’s blog, we’ll go over how to use PowerShell to manage your Microsoft Office 365 environment. You may ask yourself “Why would I want to use PowerShell when Microsoft already gives me a graphical user interface to work with?” Well, the front end of the user interface is nice for simple tasks, but the real power of Office 365 lives in the Cmdlets of PowerShell. Those commands will help you manage your domain.
Note: PowerShell has a lot to offer in the way of automation. Like any new computer language you learn, there is learning curve and it is sometimes easy to get lost. Hang in there! After learning the basic Cmdlets, you will start seeing for yourself how useful solutions can be created.
So, let’s get set up for the trip!
1. Windows 7, Windows 8, Server 2008 R2, Server 2012, or Server 2012 R2
- PowerShell. If you’re running Win7 or newer, PowerShell is already installed. If that’s not the case, you will need to download and install it. You can get PowerShell 3.0 here. WARNING: If you’re managing Exchange 2010 or earlier on premise with PowerShell, you will need to use PowerShell 2.0.
- .NET Framework. You need .NET Framework 3.51 installed.
- Microsoft Online Services Sign-in Assistant. You will need this small piece of software installed to connect to Office365. It is also necessary for running the Lync client on your desktop. You can download it here.
2. Office 365 Cmdlets
You will also need to download and install these files:
Scripting your connection
Once you have met the prerequisites above, you’re ready to actually connect to your Office365 domain. Here is the script you will need to run:
$O365Cred = Get-Credential
$O365Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $O365Cred -Authentication Basic -AllowRedirection
Import-PSSession $O365Session -AllowClobber
Connect-MsolService –Credential $O365Cred
So, what’s going on here? Here’s the breakdown, line-by-line:
- Import-Module adds the Office 365 Cmdlets into your session. This enables your local session to speak the language of Office 365.
- A dialog box is launched to ask for your Office 365 username and password, which will look something like: email@example.com. This credential information is stored in the variable $O365Cred, which lets you use it again later.
- The third command creates a new Windows PowerShell session in the cloud at https://ps.outlook.com/powershell.
- This line imports the Cmdlets from that session into your local session.
WARNING: Depending on your granted access, you could easily cause havoc in the system if you’re not careful. Be mindful of the power you have at your fingertips. If you ever have doubt about what a command might do, use the –whatif parameter available with most Cmdlets to make sure.
Okay, so you’re connected. Now what? Introducing Get-Msoluser! This is one handy command. It returns the following information about all the users in your organization:
Now, say you only want to look for user with a ‘t’ somewhere in their name. Use: Get-MsolUser -SearchString “t”.
You can also pipe Get-MsolUser output into another command such as Remove-MsolUser. For instance, say you’re doing some cleanup of accounts. You can use this command to delete all accounts with ‘Sunshine’ in their name: Get-MsolUser –SearchString “Sunshine”| Remove-MsolUser
What you DON’T what to use is this: Get-MsolUser | Remove-MsolUser. That would delete your entire organization! Remember, with great power comes great responsibility. This goes back to the warning about being careful.
Stay tuned for upcoming blogs on other useful Powershell Cmdlets. Looking for more tips and tricks on working with Office 365 instead? We’ve got plenty of great blogs on the topic.
That’s all for now!