I Need To Make A Change With My IT Support! Call (225) 706-8414

How to manage Office 365 Exchange Mailboxes using Powershell

You want to be able to manage your Exchange users in Office 365 in Powershell.

Solution

You need to install Powershell and then connect to Office 365.

  1. To begin you will need to download and install the version of Azure Active Directory Module for Windows Powershell that is appropriate to your system, ie 32-bit or 64-bit.
    http://go.microsoft.com/fwlink/p/?linkid=236298 32-Bit
    http://go.microsoft.com/fwlink/p/?linkid=236297 64-Bit
  2. Once this is installed you can begin to connect to the Office 365 environment. Below is a powershell script created to connect to Office 365 and set the correct connection strings for Exchange.
    <Code>
    Set-ExecutionPolicy Unrestricted
    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
    Connect-MsolService –Credential $O365Cred
    <Code>
  3. You can simply copy and paste this into a text document and rename the extension to .ps1.
  4. This is IMPORTANT, when you open the Powershell command line you must do it in ADMINISTRATOR MODE.
  5. When you run the script you created it will look something like this.
  6. Here you will enter the credentials for the Domain you want to control. After entering credentials it will add the cmdlets associated with your login.

Using Cmdlets to Manage a Mailbox in Powershell

From this point you will need to know some basic cmdlets for Exchange. Below is a list of commonly used cmdlets for mailboxes.

  • Get-Mailbox
  • Set-Mailbox
  • Format-List
  • Format-Table
  • Select-Object
  • Export-Csv

This is a very small list of cmdlets, more can be found through Microsoft’s website.

The most useful commands you will probably use to manage users and generate reports about users will be the Get-Mailbox and Set-Mailbox cmdlets. Let’s look at what can be done.
To find all details about all user’s mailboxes in the orginazation you can simply use:

Get-Mailbox –Resultsize Unlimited | FT

 

 

This will generate a basic list showing Name, Alias, ServerName, ProhibitSendQuota.

Powershell uses the GET and SET commands at the start of a command throughout the CLI nomenclature. The GET command would fetch results from a query where SET would change a pre-existing attribute of an item.

For syntax and examples of Get-Mailbox cmdlet click on the link below:
http://technet.microsoft.com/en-us/library/bb123685%28v=exchg.150%29.aspx

For syntax and examples of Set-Mailbox cmdlet click on the link below:
http://technet.microsoft.com/en-us/library/bb123981%28v=exchg.150%29.aspx

A little background on the | symbol. This is a pipe. It will pipe commands to another cmdlet. Above I asked to show all mailboxes and pipe it to a Format-Table cmdlet. With Format-Table you can pick and choose which properties of the users mailbox you would like to see returned. You could also pipe the command to a Format-List cmdlet. The difference between the two cmdlets is the Format-Table cmdlet put the data returned into a table and the Format-List command returns the requested data in a list format.  With the Format-List option it will return all attributes of the user’s mailbox. I suggest using this only on a per mailbox basis. To use the Export-Csv cmdlet you would need to use a get-mailbox query, then pipe which attributes you want to show up using Select-Object, then pipe it to the Export-Csv with a file location.  So let’s say we want to search all mailboxes and return the following attributes to a csv.

  • Which Database is the mailbox on?
  • Is the mailbox on Litigation Hold? If so all details.
  • What is the email address for the mailbox?
  • What is the mail quota for the mailbox?
  • What is the name of the user of the mailbox?

I’ll show this query in two ways, using full names for all cmdlets and using powershell shorthand.

Get-Mailbox –Resultsize Unlimited | Select-Object Database, LitigationHoldEnabled, LitigationHoldDate, LitigationHoldOwner, LitigationHoldDuration, UserPrincipalName, ProhibitSendReceiveQuota, DisplayName | Export-csv “C:PS_OutputPowershell.csv”

Running this command will give all the requested information in the previous questions.

Shorthand verison:

Get-Mailbox –Resultsize Unlimited | Select Database, *litigation*, UserPrincipalName, ProhibitSendReceiveQuota, DisplayName | Export-Csv “C:PS_OutputPowershell.csv”

Certain commands can be shortened like Select-Object, Format-Table, and Format-List. They would be shortened to Select, FT, and FL. If you notice all of the litigation attributes were shortened to *litigation*. Powershell DOES recognize wildcards in some contexts.

Concerned About Cyber Attacks?

CLICK HERE >

Want to Migrate to the Cloud?

CLICK HERE >
Office 365

Ready to Experience Microsoft Office 365?

Want the latest IT news directly in your inbox? Subscribe now!