Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications


Managing Office 365 with PowerShell

Post Author: Joe D365 |

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!

Prerequisites

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:

Import-Module MSOnline

$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:

  1. Import-Module adds the Office 365 Cmdlets into your session. This enables your local session to speak the language of Office 365.
  2. A dialog box is launched to ask for your Office 365 username and password, which will look something like: username@domain.onmicrosoft.com. This credential information is stored in the variable $O365Cred, which lets you use it again later.
  3. The third command creates a new Windows PowerShell session in the cloud at https://ps.outlook.com/powershell.
  4. This line imports the Cmdlets from that session into your local session.

Easy, right?

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:

UserPrincipalName
DisplayName
isLicensed

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!

Happy CRM'ing!

Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

PowerObjects Recommends