TechRant
Connecting to Office 365 using Powershell … It’s the future ya’ know …
Apr 07, 2014

I get asked about Powershell alot. Questions like:

“is it really all that? Is it that easy to use? Is it that hard?” and on and on and on….

Powershell is one of those ” Love it or Hate It ” kind of things. You just have to believe, believe that it will all be o.k. as long as you don’t move suddenly or try anything crazy, after all, it is a first date, you know, the kind where you have to decide whether or not you are going to actually make it through, or make excuses on your way out the door.

The reality of Powershell is not that scarry, not at all. As a matter of fact, it is something very different for most Windows administrators. It is an opportunity to shine, and to make your networks do things that you never thought they could do, if you are willing to do three things:

1. Stop –> Stop the madness that has plauged windows administrators since the dawn of the GUI, which is the fear of the dark, the fear of the command line.

2. Drop –> Drop your preconceived notions about the fact that the only way to automate a Windows environment is to let a third party software vendor do it for you.

3. Roll –> Roll with the waves of change sweeping over the Windows landscape, the changes that are making Powershell a requirement for all Windows administrators who want to be able to say that they are still Windows administrators in another year or two.

Now that I have your attention, and you have been warned, let’s take a look at one small aspect of Powershell that I am being asked about all of the time these days:

Set up your computer to use Powershell

First, you need to set up your computer to be able to use Powershell. This only needs to be done the first time and involves downloading some tools from the Microsoft website.

Here is a link to the tools you will need to install: http://technet.microsoft.com/en-us/library/jj151815.aspx

You only need to install either the 32bit OR the 64bit tools. If you do not know which tools to install, select the 32bit ones.

Start the Powershell command line

Now that you have the tools installed, you can go ahead and open Powershell. The easiest way to do this is to press the start button and simply type powershell.

 Finding Powershell on your computer

 

Using Powershell

Now that you have a Powershell Window open, you can connect to Microsoft Office 365.

First, you need to create a connection to Office 365. You can do this by typing (or copying/pasting) the following into Powershell.
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “https://ps.outlook.com/powershell/” -Credential $cred -Authentication Basic -AllowRedirection

Note: To paste into Powershell you use Right Click. Here is a great video on how to use Copy and Paste in Powershell

Now you simply type in the username and password for an Administrator account in your Office 365 and click OK.

 Authenticating into Office 365 via Powershell

 

You are now authenticated into Office 365. The last step is to connect up to Office 365 by using the following Powershell command:

Import-PSSession $session

Import-PSSession

Once you hit enter, you will begin to see some activity as shown below:   ( I am using Windows PowerShell ISE, so I am seeing the module creation activity at the top of the PowerShell window )

 Creating implicit remoting module

You are now connected to Office 365 via PowerShell.

You may now type a variety of commands to administer Office 365 as needed. I have started typing a typical command “get-“ in the picture below in order to show you the auto complete feature of PowerShell. You will see that I am presented with a scrollable list of commands to choose from that start out with “get-“. As I select one, I am presented with a pop out window that shows the full syntax for the command.

Get command with pop out window

When I choose to use the Get-Mailbox command, I am shown a complete listing of all of the mailboxes in my Office 365 tenant.

Setting up Powershell to connect to Office 365

Taking things one step further, I can use the following commands strung together to quickly see if any of my users have forwarding set up on their mailbox. The DeliverToMailboxAndForward  will tell you if the button is selected to keep a local copy before forwarding.

Get-Mailbox | select UserPrincipalName,ForwardingSmtpAddress,DeliverToMailboxAndForward

 Get-Mailbox with select UPN and Forwarding

If you wanted to export this to a CSV then simply append the Export-CSV cmdlet as shown in the example below.

Get-Mailbox | select UserPrincipalName,ForwardingSmtpAddress,DeliverToMailboxAndForward | Export-csv .\users.csv -NoTypeInformation

Another script that may be of value to you is the following, which will allow you to export all mailbox sizes to a CSV file using PowerShell. Enter the following script into PowerShell if you want to simply create a listing on the screen:

get-mailbox | get-mailboxstatistics | select DisplayName,ItemCount,TotalItemSize

Get-Mailbox with get-mailboxstatistics

Enter the following into PowerShell if you want to output to a CSV file:

get-mailbox | get-mailboxstatistics | select DisplayName,ItemCount,TotalItemSize | export-csv “MailboxSizes.csv”

 

 

Comments:

No comment found.

Leave a new comment

Search

Please enter a keyword.

Recent Posts

Categories

Copyright © 2017 New Horizons Worldwid, Inc. All rights reserved.
Version: 5.7.1.0