You are currently viewing Connect to Office 365 PowerShell All-in-one Script
Connect to Office 365 PowerShell

Connect to Office 365 PowerShell All-in-one Script

In this post, you’ll learn how to connect to Office 365 PowerShell All-in-one Script to connect multiple Office 365 services in a single window.

I believe you must be interacting with PowerShell and know its benefits if managing Office 365, as many tasks are not available in the Admin centre.

Firstly, you’ve to connect session in PowerShell for particular services before executing any operation command. It’s really pain, isn’t it?

Overview

I started to create an All-in-one PowerShell script in which we’ll first store the credentials in an encrypted format as a result none can read or use that and you don’t have to re-enter the credentials every time. After that, we’ll use the existing saved credentials to connect multiple Office 365 services.

This was a repeated task, so it’s going to save a lot of time and efforts. Currently, PowerShell is the most important skill if you’re administrating Office 365 related services that’s why you should learn.

Let’s start creating this all-in-one PowerShell script step by step…

Prerequisite to Connect to Office 365 PowerShell

You need to meet the below prerequisites on your system to connect the Office 365 PowerShell services:

  • Microsoft .NET Framework 4.5 or above,
  •  Windows Management Framework 3.0 or 4.0,
  •  PowerShell version 5.1 or later,
  • 64-bit version of Windows,
  • Microsoft Online Services Sign-In Assistant (If not running Windows 10)
  • Member of Microsoft 365 admin role

Install the Required Modules

If you’ve installed the below modules already on your system in that case feel free to skip and jump to the next section.

Install windows azure active directory module for PowerShell :

Here, I want to inform you that currently a new module “Azure Active Directory PowerShell for Graph” has been publicly released by Microsoft in replacement of the old MSOnline module.

Still, MSOnline old module in use because all functions are not available currently in the newer module. But Microsoft encouraged us to use the newer module and very soon all functions would be available.

ModuleDescription
AzureAD (Newer)Azure Active Directory PowerShell for Graph​
MSOnline (Older)​MSOnline PowerShell (deprecated)​
AzureAD PowerShell Module list

Install Microsoft Office 365 services modules by below commands (Execute with Local Admin privilege):


# Set Execution Policy to RemoteSigned
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

# Install Azure Active Directory PowerShell for Graph module (Newer)​
Install-Module -Name AzureAD​

​
# Install Microsoft Azure Active Directory Module for Windows PowerShell (Older)​
Install-Module -Name MSOnline​


# Install Exchange Online PowerShell V2 module​
Install-Module -Name ExchangeOnlineManagement​

​
# Install MicrosoftTeams Module​
Install-Module -Name MicrosoftTeams​


# Install SharePoint Online Management PowerShell Module

Now you need to install below SharePoint Online module by download an MSI setup file:​

https://www.microsoft.com/en-us/download/details.aspx?id=35588​


# Install Skype for Business Online PowerShell Module

Skype for Business Online Connector is currently part of the latest Teams PowerShell module. ​So no need to install any additional module and you may check the below link for more details.

https://www.microsoft.com/en-us/download/details.aspx?id=39366

Connect to M365 Subscription :

Microsoft has set up multiple different environments for the below country’s. If your services hosted in that location then you’ve to specify the environment name while connecting:

Connect to AzureAD with your M365 subscription :

Office 365 Cloud EnvironmentConnect Command
Office 365 WorldwideConnect-AzureAD​
Office 365 operated by 21 VianetConnect-AzureAD -AzureEnvironmentName AzureChinaCloud​
Office 365 GermanyConnect-AzureAD -AzureEnvironmentName AzureGermanyCloud
Office 365 U.S. Government DoD and Office 365 U.S. Government GCC HighConnect-AzureAD -AzureEnvironmentName AzureUSGovernment
Office 365 Cloud Environment Names

​Prepare Single Script to Connect to Office 365 PowerShell All Services

Firstly we’ve to create a variable to take input of credentials and save in encrypted format. So that only on your system with your user profile can be used to authenticate the connection request.

  • Here, we’re storing admin’s email ID in variable $username
# Store Office 365 admin user name value
$username = "[email protected]"
  • You’ve to use the below commands to take input of password from the user and convert in secure string then save in a text file.
#Save password in secure string inside txt. (execute once and then comment out below line)
read-host -assecurestring | convertfrom-securestring | out-file "$ENV:UserProfile\PowerShell Scripts\Connect M365\M365_Cred.txt"

If you open the exported file, will see the saved password in the below-encrypted format, as a result, none can guess the password. That’s why we convert it to a secure string before export to the file.

connect to Office 365 PowerShell
saved password in the encrypted format

This line will execute only one time to take the password after that you need to comment out so that it doesn’t ask again to enter the password in the next execution. When your password gets change after that please execute again to update the password in the script.

#read-host -assecurestring | convertfrom-securestring | out-file "$ENV:UserProfile\PowerShell Scripts\Connect M365\M365_Cred.txt"
  • Now you need to store the saved password back and decrypt before save it by using ConvertTo-Securestring .
#Get saved password in $password varible
$password = Get-Content "$ENV:UserProfile\PowerShell Scripts\Connect M365\M365_Cred.txt" | ConvertTo-Securestring
  • Now you’ll create a new object of type System.Management.Automation.PSCredential to store the user name and saved password value in a secure format in a variable $credential.
# Store existing Username and password variable value in $Credential varible to re-use
$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

After that you may use this variable $credential to connect Office 365 PowerShell any services like the below commands:

Connect to Azure AD Office 365 PowerShell

#Connect to AzurrAD (Newer)
Connect-AzureAD -Credential $credential

#Connect to O365 (Older)
Connect-MsolService -Credential $credential

Connect to SharePoint Online Office 365 PowerShell

#Connect to SharePoint
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Connect-SPOService -Url https://example-admin.sharepoint.com -credential $credential

Connect to Skype for Business Online PowerShell

#Connect to Skype for Business Online.
Import-Module SkypeOnlineConnector
$sfboSession = New-CsOnlineSession -Credential $credential
Import-PSSession $sfboSession

MS Teams Office 365 PowerShell

#Connect to MS Teams
Connect-MicrosoftTeams -Credential $credential

Connect to Exchange Online PowerShell

# Connect to Exchange Online.
$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange `
 -ConnectionUri "https://outlook.office365.com/powershell-liveid/" `
  -Credential $credential `
  -Authentication "Basic" 
Import-PSSession $exchangeSession -DisableNameChecking -ErrorAction silentlycontinue -AllowClobber

Security & Compliance Center PowerShell

# Connect to Security & Compliance Center.
$SccSession = New-PSSession -ConfigurationName Microsoft.Exchange `
 -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ `
  -Credential $credential `
  -Authentication "Basic" 
  -AllowRedirection
Import-PSSession $SccSession -Prefix cc

Now let’s combine all in one script and see how it’s look like:


### ​Prepare Single Script to Connect to Office 365 PowerShell All Services ###

# Store O365 admin user name vaule
$username = "[email protected]"

#Save password in secure string inside txt. (execute once and then comment out below line)
read-host -assecurestring | convertfrom-securestring | out-file "$ENV:UserProfile\PowerShell Scripts\Connect M365\M365_Cred.txt"

#Get saved password in $password varible
$password = Get-Content "$ENV:UserProfile\PowerShell Scripts\Connect M365\M365_Cred.txt" | convertto-securestring

# Store existing Username and password variable value in $Credential varible to re-use
$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

#Connect to AzurrAD (Newer)
Connect-AzureAD -Credential $credential

#Connect to O365 (Older)
Connect-MsolService -Credential $credential

#Connect to SharePoint
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Connect-SPOService -Url https://example-admin.sharepoint.com -credential $credential

#Connect to Skype for Business Online.
Import-Module SkypeOnlineConnector
$sfboSession = New-CsOnlineSession -Credential $credential
Import-PSSession $sfboSession

#Connect to Teams
Connect-MicrosoftTeams -Credential $credential

# Connect to Exchange Online.
$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange `
 -ConnectionUri "https://outlook.office365.com/powershell-liveid/" `
  -Credential $credential `
  -Authentication "Basic" 
Import-PSSession $exchangeSession -DisableNameChecking -ErrorAction silentlycontinue -AllowClobber

# Connect to Security & Compliance Center.
$SccSession = New-PSSession -ConfigurationName Microsoft.Exchange `
 -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ `
  -Credential $credential `
  -Authentication "Basic" 
  -AllowRedirection
Import-PSSession $SccSession -Prefix cc

You may further check this M365 page for more Office 365 related learning resources.

Summary

In conclusion, you can execute this single script (All-in-one) to connect to Office 365 PowerShell multiple services in one go and can execute any O365 service-related commands without worry thinking that a specific service connection has been established or not. In this way, you can save valuable time and efforts. Try and share your feedback. 🙂

This Post Has One Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.