Top 6 PowerShell Commands for Managing Office 365

Top 6 PowerShell Commands for Managing Office 365

If you’re new to PowerShell, you should definitely consider learning the basics. This will help you in your day-to-day tasks as an Office 365 administrator.

Les 6 principales commandes

In this section, I’m going to explain a little more about the basics of PowerShell and how you can get started today.

PowerShell Basics

The great thing about PowerShell is that it’s easy to understand key concepts that can be reused over and over again.

In the days of command-line scripting, we had good reason to fear the command line. In those environments, there simply isn’t a common standard for how to do things.

One command may take parameters one way, while another may take them completely different. In some cases, command-line tools present their own environment (like netsh or ntdsutil). Figuring out how to get help on the command itself can be very complex. And with VBS scripts, you’d be in the wild west. Some scripts may require parameters, while others may even ask for user input when you run them. While others may not use parameters at all.

Concept

PowerShell, on the other hand, works very differently. Once you understand how PowerShell commands work, you can reuse that knowledge.

PowerShell cmdlets work primarily on a Verb-Noun pair:

  • The verbal component is the part of the noun that defines the action of the command.
  • The noun component is the part of the noun that defines what the action will be performed on.

For example, most PowerShell modules have Get- cmdlets that retrieve information about components like mailboxes. They also have verbs for creating, updating, or deleting components. This means you can create a New-Mailbox, update it with additional information using Set-Mailbox, and then get a list of all your mailboxes with Get-Mailbox.

Input in PowerShell is just as consistent. PowerShell cmdlets can accept parameters. These are added after the cmdlet, in any order you choose. And they can be easily discovered using tab-based autocompletion. A parameter has the form -Parameter followed by the data you want to provide. For example, if you want to retrieve information about a mailbox, you would use Get-Mailbox -Identity steve@practical365.com.

Interpret the results

PowerShell output is a little more complex to understand. But you don’t need to know everything about how it works to run cmdlets. Because PowerShell is object-oriented, the output of a cmdlet isn’t purely textual. Rather, it’s an object—which can be a list of objects, each containing attributes with information about each object. For example, PowerShell can display a list of mailboxes in tabular format with only the important information. However, the object contains all the information the cmdlet provided as output. Running Get-Mailbox, for example, will contain all the mailbox information. This information will be visible if you examine the mailbox using the Exchange Admin Center and more.

Finally, you need to know how to connect to Office 365 if you want to manage it. You’ll find everything you need to know in our Office 365 Admin Portals and PowerShell Connections tutorial. Personally, I use the Connect to Office 365 script on my workstations to easily connect to each service.

Useful cmdlets you need to know

Now, it’s helpful to know some common cmdlets and command combinations you can use. In no particular order, here are six that I use almost daily.

Select-Object

When you use cmdlets like Get-Mailbox, you get a pre-formatted, filtered result. Sometimes, however, you just want to get specific information. Especially if you want to export it to a CSV file for further analysis.

Select-Object is perfect for this. Use the pipe symbol | after the cmdlet you ran to pass the output object to Select-Object. Then enter the attribute names, followed by commas to select only those attributes:

Get-Mailbox | Select-Object DisplayName, PrimarySMTPAddressGet-MailboxStatistics | Select-Object DisplayName,TotalItemSize

If in doubt, simply use Select * to select all attributes before collapsing.

Where-Object

When you use a cmdlet like Get-Mailbox without any parameters, you receive a complete list of all objects. With Get-Mailbox, this can be all mailboxes. You may want to filter this list based on how an attribute is set. For example, you can find all mailboxes that are hidden from the address book. You can use Where-Object to achieve this.

In the Where-Object cmdlet, each line of the output object is evaluated based on the defined condition. You can use comparison operators like -eq or -like to evaluate whether an attribute matches, is similar, does not match, is less than, or is greater than. As we did with Select-Object, we can specify the attributes to check. However, in the Where-Object cmdlet, we prefix the attribute name with the special characters $_. as shown below:

Get-Mailbox | Where-Object {$_.HiddenFromAddressBook –eq $False}
Get-MsolDomain | Where-Object {$_.Authentication -eq "Federated"}

Foreach-Object

The foreach loop allows you to access each line of the output object. And it executes the same set of commands on each line. This is a very simple technique and fundamental to all but the simplest scripts:

Foreach-Object ($mailbox in (Get-Mailbox))
{
	Get-MsolUser –UserPrincipalName $mailbox.userprincipalname
}

Start-Transcript.

Is this the command I ran last week? With Start-Transcript, you can check what you did and maybe spot the mistake you made.

Start-Transcript starts transcribing a text file that records all the cmdlets (and text output) from your PowerShell session. You don’t even need to think about the file name. If you simply use the cmdlet without any parameters, it will create a new file in your Documents folder with the machine name and timestamp. When you’re done making changes, use Stop-Transcript to stop logging:

Start-Transcript
# Do the thing that you were planning to do…
Stop-Transcript
# Next week, check the logs when you are asked to show them what you did

Export-CliXML.

Our penultimate cmdlet is one of the most useful tools if you’re working with Office 365. It’s Export-CliXML and its sister cmdlet Import-CliXML.

This pair of cmdlets allows us to capture the complete output into an XML file – a point-in-time snapshot of the state of Office 365.

In our example below, we can use the Get-Mailbox cmdlet to export whatever the cmdlet would return to the Mailboxes.XML file. If we then use Import-CliXML at a later date to import the Mailboxes.XML file, we can then treat it as if we were running Get-Mailbox over and over again. I will be using this set of cmdlets on a daily basis to capture information from an Office 365 tenant. And thus run scripts on that data without needing to access the tenant itself.

Get-MsolUser -All | Export-Clixml .\MsolUser.XML
Get-Mailbox –ResultSize Unlimited | Export-CliXML .\Mailboxes.XML
# Copy it somewhere else
Import-CliXML .\Mailboxes.XML

Get-Help.

Finally, there’s the most useful cmdlet in your PowerShell toolbox, Get-Help. If you haven’t used a cmdlet in a while, Get-Help provides detailed information with examples. In the example below, you’ll see my most useful parameter for Get-Help. This will remind me not only of the parameters to use, but also of typical values. Detailed provides more information about the cmdlet, including detailed information about each parameter. While -Online redirects us to the Microsoft Docs version of the cmdlet’s documentation.

Get-Help Get-MsolAdministrativeUnit –Examples
Get-Help Get-MsolAdministrativeUnit –Detailed
Get-Help Get-SPOTenant -Online

What to do next ?

If you’re not using PowerShell today, check out our dedicated PowerShell section to learn a little more.

If you want to start using these examples, consider doing the following…

  • Use Start-Transcript and Export-CliXML to keep a copy of the before and after state and a log of what you did.
  • Choose a few Get- commands that seem useful to you and use Get-Help to learn more about them. Can’t find any? Try Get-Command
  • Use Select-Object in combination with Export-CSV or Out-gridview to allow you to easily provide reports from commands or view them on screen.
  • Use Where-Object and Foreach-Object to automate your first task!
PowerShell – An Introduction to the Basics

PowerShell – An Introduction to the Basics

Is using PowerShell for web access practical?

PowerShell Web Access: Is It Practical?

New-DistributionGroup: A Misunderstood PowerShell Cmdlet

New-DistributionGroup : A misunderstandood PowerShell Cmdlet

Adapt your Exchange Online scripts to use Get-ExoMailbox

Adapt your Exchange Online scripts to use Get-ExoMailbox

Scroll to Top