You want to be able to manage your Exchange users in Office 365 in Powershell.
You need to install Powershell and then connect to Office 365.
- 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.
- 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.
$O365Cred = Get-Credential
$O365Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $O365Cred -Authentication Basic -AllowRedirection
Connect-MsolService –Credential $O365Cred
- You can simply copy and paste this into a text document and rename the extension to .ps1.
- This is IMPORTANT, when you open the Powershell command line you must do it in ADMINISTRATOR MODE.
- When you run the script you created it will look something like this.
- 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.
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:
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:
For syntax and examples of Set-Mailbox cmdlet click on the link below:
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.
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.