Access and Manage Office 365 with PowerShell

As businesses large and small move operations to the Microsoft cloud in the form of Office 365, administrators have to face some limitations in the user interface. While very simple to use for basic administrative tasks, some things can only be done using PowerShell, while other tasks are far faster and easier using this scripting tool as well.

In the blog, I will show you how to set up your PowerShell environment to quickly connect to your Office 365 service and help you carry out these functions quickly and easily.

The steps will be as follows:

  1. Check your Execution Policy in PowerShell
  2. Check for, or create, a PowerShell Profile
  3. Edit the Profile to add the needed function
  4. Test the connection

Using Windows 7 or Windows 8, you will find that PowerShell is part of the operating system environment. In Windows 7 there is a pinned shortcut on the taskbar. Opening PowerShell either using this pinned application or by using the Start Menu is the first step. While in PowerShell we need to examine your current settings for executing scripts. This is referred to as the Execution Policy and can be retrieved by typing “get-ExecutionPolicy”. If your response is something other than RemoteSigned you need to set a new policy by typing: “set-ExecutionPolicy RemoteSigned”. You can then confirm that your setting is correct by repeating the “Get-ExecutionPolicy” command. There are other choices in PowerShell, but this will suffice for our project. For more information in PowerShell type “help about_Execution_Policies”.

Next, we want to locate the profile that is associated with your PowerShell environment. To learn more about profiles, type “help about_profiles”. To locate your current profile, if it exists, type $profile. What returns should be a string that shows the complete path to your current profile. When you learn about profiles in PowerShell you will see that there are several locations in which these profiles can be kept and each has a different impact on how stored functions and scripts might be accessed on your machine.

If no profile exists, you will need to create one. You can test for the existence of a profile by typing, “test-path $profile”. If the response is False, we need to create one, if the response is True, you can simply type $profile to check the path as I pointed out above.

To create a new profile type the following:

if (!(test-path $profile)) {new-item -type file -path $profile -force}

This command tests for the existence of a profile and if none (!) exists, creates a new one.

Now the heavy lifting, which is to create a script or function to help you automatically log on and connect to your Office 365 service. First type, “notepad $profile”. While in Notepad, type the following code:


function admin365

{        $LiveCred = Get-Credential;$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $LiveCred -Authentication Basic -AllowRedirection;Import-PSSession $Session        }


Don’t forget the braces, {}.

This code does three things: 1.) it creates a function called “admin365″, 2.) prompts you for your credentials on Office 365 in the form of a dialog box, and 3.) creates a new session to connect you to your Office 365 environment and extends the commands available for you to manage your Exchange environment. Notice the use of a semicolon to separate commands in the string. Items starting with a “$” indicate variables in PowerShell.

Save the notepad file. Exit from PowerShell and restart a new PowerShell session, which will now utilize your new profile. Since this profile has defined a function for your use, call it from the command line by typing “admin365”. A dialog box will pop up. Enter your credentials, click OK and wait for the connection strings to finish. Once done, you can type “get-command” to see all the new commands that are now at your disposal to help you manage your environment.

Now you can do work in Office 365 using PowerShell. Want to get a list of users? Type, Get-User. Want to see information concerning a user’s mailbox? Type, “Get-mailbox username | fl”. To set mailbox permissions, type “set-mailboxpermission username -Accessrights moreparameters”. For what follows in parameters see “help set-mailboxpermission”. To see all the commands available, type “get-command”.

Using PowerShell can be very helpful, but there is much to learn about this scripting environment.

For more Microsoft Technical Training information visit or call 1-855-575-8900.