1. Using two PowerShell console to be able to fulfill
Consider the following scenario: we want to use PowerShell for the following task- create a new Office 365 user and assign to the user additional email address (Alias).
Pay attention to the fact that this “task” requires access to two environments:
- User management (create a new user) is implemented by creating a remote PowerShell session to Office 365
- Email address management (add email alias), is implemented by creating a remote PowerShell session to Exchange Online.
The standard way to we use to execute this task, include few disadvantages:
- Using two PowerShell console to be able to fulfill this task we usually use two PowerShell consoles:
A. Creating a remote PowerShell session to Office 365 by using the shortcut “Microsoft Online Services Module for Windows PowerShell” (that includes a command that imports the Office 365 cmdlets to the PowerShell console).
B. Creating a remote PowerShell session to Exchange Online by using the built-in Windows PowerShell console. - Dealing with “long” set of PowerShell Commands The command that we use for creating the remote session to Office 365 is quite simple (Connect-MsolService) but for creating the remote PowerShell session to Exchange Online, we need to use a “block” or a set of PowerShell commands. Most of the time, we save the PowerShell set of command in a text file, and each time that we need to create the remote session, we copy and paste the required “block” of the PowerShell commands.
I can mention additional “obstacles” such as: spelling or syntax mistake when trying to enter the PowerShell commands manually and more, but I think you get the idea.
Using a PowerShell script
In general, using a PowerShell script includes many advantages that can make our life easier. In this article, we will demonstrate how to use a PowerShell script for creating Remote PowerShell connection. In addition, the site includes articles that deal with other daily management tasks using PowerShell scripts.
1: Download and install required components
Before we can start the remote PowerShell session to office 365, we need to download the required cmdlets. The additional pre-requirement is to install the: Office 365 sign in assistant. You can find the required software component using the following link: Manage Azure AD using Windows PowerShell

2: Using the PowerShell console
3: Change the default PowerShell Execution Policy
Set-ExecutionPolicy Unrestricted

Running the remote connection script
1. Create “script” folder
It’s recommended to create a dedicated folder that will hold the scripts that we use. For example- on drive C: created a folder named: scripts
2. Download the script
Download the Conenct0365.ps1 script, and save it in the scripts folder.
3. Open the PowerShell console
Open the PowerShell console (Start menu > All Programs > Accessories > Windows PowerShell> Windows PowerShell ).
4. Go to Script folder
Type the command: cd c:\scripts
5. Running a PowerShell script
For running a PowerShell script we first need to type “ .\ “ Charters. By using these characters, we are “telling” to the PowerShell that we want to run a script. You can manually type the “full” script name, or other option is to use the autocomplete PowerShell feature, that enable us to type the first characters of the script file name, and hit the TAB key (The “full name” of the script will be completed automatically).
6. Using script menu options
The script displays a menu with 3 options. To choose one of the options type the menu number.
Menu 1 – Login
Login in using your Administrator credentials: choosing this option, will activate set of PowerShell commands that create a remote session to Office 365 and Exchange Online.
The script includes a “sample string” that populate the username filed in the authentication window ([email protected]<Your Domain>.onmicrosoft.com). You can edit the script and replace the sample string by using your credentials. Enter your credentials and…
Walla!, you are now connected.
Menu 3 – Exit
After the remote connection was created, you need to “exit” the script menu, and start to use the required PowerShell commands. If you want to verify that you are connecting to booth of the environments you can use the following options:
Test the connection to Exchange Online by using the command: Get-Mailbox
Test the connection to Office 365 by using the command: Get-MsolUser
Menu 2 – Disconnect the remote PowerShell session
Best practice is to disconnect a remote PowerShell session after we have finished using the required management tasks. For disconnecting the remote PowerShell session choose “2” in the menu
Script PowerShell command description
If you want to read more information about the PowerShell commands that we use in the script, read the following section.
$user = “[email protected]
The use for this command is to simplify that part in which we need to enter our user name credentials. The variable named: $user, will display a predefined user name credentials. You can customize this value by editing the script, and enter your specific user name credentials.
$cred = Get-Credential -Credential $user
In this command we create a variable named: $cred that “contain” the command:
- Get-Credential – this PowerShell command will create a pop out windows that enable us to type our credentials
- We use the variable $user for pre populates the “user name section” in the authentication dialogue box. When we finish the part of the “password entry”, the information will be saved in the $cred variable, and will be used in next PowerShell commands.
Import-Module MSOnline
The “Microsoft Online Services Module for Windows PowerShell” shortcut includes a command to import Office 365 cmdlets to the PowerShell console. In case that we don’t want to use this option, for example when we using the standard windows PowerShell console, the command will import Office 365 cmdlets to the PowerShell console.
Connect-MsolService -Credential $cred
The command that we use for creating the remote PowerShell session to office 365. We use the $cred variable for providing the required credential.
$ExchangeURL = “https://ps.outlook.com/powershell/
Create a variable that will store the URL address of the target Exchange Online server. After our credentials were verified and approved, the request will be redirected to the specific Exchange Online server that hosts the organization mailboxes.
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $ExchangeURL -Credential $cred -Authentication Basic –AllowRedirection
The PowerShell command set that creates the remote connection to Exchange Online.
Import-PSSession $session
Summery
In this article we review
- The required first time configuration that we need to implement (download the required software + Change the Default PowerShell console Execution policy).
- How to run a script from the PowerShell console.
- How to use the connect-o365.ps1 script, to create a remote PowerShell session.
Additional reading
We really want to know what you think about the article
This is a great script, the menu is a nice feature. I’d like to make the “exit” menu a bit clearer so that people that don’t read the notes realize that choosing “exit” is just exiting the script menu, but not your posh session. Is it possible to do that? Then how do you get back to the script menu to disconnect properly?
Awesome – saves me precious time. The whole Site is one big “have-to-know” !
Well done :).
Woot! Perfect. This and the https://o365info.com/manage-office-365-users-password-using/ link solved a major issue for me!
What would I do if i needed to run a custom ps1 script in 365? How would I do this?
I am getting error as “There is something wrong with the global administrator credentials”. I am sure that my username and password is correct.
Hi,
Really like you scripts, however now we are updating our clients and global admins to have multi factor authentication, the scripts no longer appear to be able to connect to the tennat’s
Is there any update to these scripts for MFA ?
Corey