Setup scenarios for Office 365 PowerShell

Use these PowerShell scenarios to connect to Office 365 services, get help, and set the execution policy for scripts.

Connect to Office 365 services using PowerShell

To get connected with Office 365 services, you must first install or import the latest PowerShell modules and then create an authenticated connection.

Connect to Azure Active Directory with the Windows Azure Active Directory Module

To install the Windows Azure Active Directory Module and connect with either an account user name and password or with multifactor authentication (MFA), see Connect to Office 365 PowerShell.

Connect to Azure Active Directory with the Azure Active Directory V2 module

To install the Azure Active Directory V2 module and connect with either a user name and password or with MFA, see Connect with the Azure Active Directory V2 PowerShell module.

Connect to Exchange Online

To connect to Exchange Online with an account user name and password, see Connect to Exchange Online PowerShell.

To install the Microsoft Exchange Online Remote PowerShell Module and connect with MFA, see Connect to Exchange Online PowerShell using multi-factor authentication.

Connect to SharePoint Online

To install the SharePoint Online Management Shell and connect with either an account user name and password or with MFA, see Connect to SharePoint Online PowerShell.

Connect to Skype for Business Online

To connect to Skype for Business Online with an account user name and password, see Connecting to Skype for Business Online by using Windows PowerShell.

Use these steps to connect to Skype for Business Online with MFA:

  1. To install the current Skype for Business Online PowerShell module, go to Skype for Business Online, Windows PowerShell Module, click Download, and then run the downloaded file and install it.

    You only need to do this once on each computer from which you are managing Skype for Business Online.

  2. Open a Windows PowerShell command prompt.

  3. Fill in the account name between the double-quotes, removing the instruction text and the < and > characters.

    $acctName="<User Prinicipal Name of an administrator account,  example: belindan@contoso.com>"
    
    #Create a Skype for Business Online session
    $sfboSession = New-CsOnlineSession -UserName $acctName
    
    #Import the Skype for Business session
    Import-PSSession $sfboSession

    Run the resulting commands from the Windows PowerShell window.

  4. Type your account password in the SkypeForBusinessAuth dialog box, and then click Sign in.

  5. Follow the instructions in the SkypeForBusinessAuth dialog box to provide additional authentication information, such as a verification code, and then click Sign in.

Connect to Azure Active Directory and Exchange Online

These PowerShell commands will connect you to Azure Active Directory and Exchange Online after you have installed the Windows Azure Active Directory Module.

#Connecting to Exchange Online and Azure Active Directory

#Get administrator account credentials
$credential = Get-Credential

#Connect to Azure Active Directory  
Connect-MsolService -Credential $credential

#Create an Exchange Online session
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $credential -Authentication Basic -AllowRedirection

#Import the Exchange Online session
Import-PSSession $ExchangeSession 

Tip: To get this PowerShell command block as a script file (PS1), click here.

Connect to all of the Office 365 workloads

To get connected with all of the Office 365 workloads in the same PowerShell window, you must first import or install the relevant modules. This PowerShell command block connects to Azure Active Directory, Skype for Business Online, SharePoint Online, and Exchange Online.

Fill in the organization name and then run this set of PowerShell commands when signing in with an account user name and password:

#Connect to Office 365 Cloud Services using PowerShell

#Specify your organization name, which is the first part of the name after the "@" character in an account user name. 
#For example, for the account user name belindan@contoso.onmicrosoft.com, the organization name is "contoso".
#Replace everything within the quotes, including the < and > characters, with the correct name. 
$orgName="<organization name>"

#Capture administrative credential for future connections.
$credential = Get-Credential

#Connect to Azure Active Directory.
Connect-MsolService -Credential $credential

#Create a Skype for Business Online session.
$sfboSession = New-CsOnlineSession -Credential $credential

#Import the Skype for Business Online session into your local Windows PowerShell session.
Import-PSSession $sfboSession

#Connect to SharePoint Online.
Connect-SPOService -Url https://$orgName-admin.sharepoint.com

#Create an Exchange Online session.
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $credential -Authentication "Basic" -AllowRedirection

#Imports the Exchange Online session into your local Windows PowerShell session.
Import-PSSession $ExchangeSession

Fill in the account name and then run this set of PowerShell commands when signing in with multifactor authentication:

#Specify an account name. 
#Replace everything within the quotes, including the < and > characters, with the correct name. 
$acctName="<User Prinicipal Name of an administrator account,  example: belindan@contoso.com>"

#Extract the organization name from the account name
$orgName= ($acctName).Substring($acctName.IndexOf("@")+1, $acctName.IndexOf(".") - $acctName.IndexOf("@") - 1 )

#Establish Online Services connection to Office 365 Management Layer
Connect-MsolService

#Create Skype for Business session
$sfboSession = New-CsOnlineSession -UserName $acctName

#Import the Skype for Business session command
Import-PSSession $sfboSession

#Connect to your SharePoint Online services.
Connect-SPOService -Url https://$orgName-admin.sharepoint.com

#Create an Exchange Online session.
Connect-EXOPSSession -UserPrincipalName $acctName

You will be prompted four times to specify an account user name and password and MFA information, such as a verification code.

Getting help with PowerShell

Sometimes you need to get additional help for PowerShell. Fortunately, PowerShell has built-in help content to guide you.

Show the help files for a PowerShell cmdlet

To get more information on a specific cmdlet, for example Get-Process, you can run the following.

#Show the help files for a PowerShell command.

Get-Help Get-Process

Bring up the TechNet article for the PowerShell cmdlet

To open the online syntax reference on TechNet for a specific cmdlet, for example Get-Process, run the following command.

#Goes online to the TechNet article for the PowerShell cmdlet.

Get-Help Get-Process -Online

Note: This requires Internet access to work correctly.

Show examples for a PowerShell command

To display example syntax for a specific cmdlet in the PowerShell window, for example Get-Process,, run the following command.

#Shows examples for a PowerShell cmdlet.

Get-Help Get-Process -Examples

Update your PowerShell help files

PowerShell help content is updated on a regular basis. To get the latest help content, you can run the Update-Help command. You may see the following message after running this command: "The Update-Help command downloads the most current Help files for Windows PowerShell modules, and installs them on your computer." Press Y and allow the module to update your help files. This will only take a few moments to complete.

#Update your PowerShell help files.

Update-Help

Tip: To get this PowerShell command block as a script file (PS1), click here.

Set the script execution policy

PowerShell scripts are PowerShell commands that are run from one or more text files. Scripts allow you to automate common tasks within Windows, Microsoft applications, and of course Office 365 services. Because malicious scripts can potentially harm your computer by executing unintended commands, Microsoft has protected you by disabling PowerShell scripts by default. You must set the PowerShell execution policy according to your needs. The execution policy settings are as follows:

  • Restricted

    Does not load configuration files or run scripts. "Restricted" is the default execution policy.

  • RemoteSigned

    Requires that all scripts and configuration files downloaded from the Internet be signed by a trusted publisher.

  • AllSigned

    Requires that all scripts and configuration files be signed by a trusted publisher, including scripts that you write on the local computer.

  • Unrestricted

    Loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the Internet, you are prompted for permission before it runs.

  • Bypass

    Nothing is blocked and there are no warnings or prompts.

  • Undefined

    Removes the currently assigned execution policy from the current scope. This parameter will not remove an execution policy that is set in a Group Policy scope.

Note: It is important that you only download or use PowerShell scripts from a trusted source especially when setting the policy as ‘Unrestricted’. It is also recommended for security reasons that you revert any changes to Restricted unless you have an ongoing need to run scripts.

Executing the Set-ExecutionPolicy command can only be done in a PowerShell window opened using “Run as administrator.” This is also known as an elevated PowerShell session.

Set execution policy as remote signed

This command will require that all scripts and configuration files downloaded from the Internet be signed by a trusted publisher. You can replace RemoteSigned with your desired execution policy name to alter the policy applied.

#Set execution policies as remote signed.

#This command will display your current execution policy. This is worth noting if you wish to revert back at a later point.
Get-ExecutionPolicy

#This command will set a policy that requires that all scripts and configuration files downloaded from the Internet be signed by a trusted publisher
Set-ExecutionPolicy RemoteSigned

Tip: To get this PowerShell command block as a script file (PS1), click here.

Set execution policy as restricted

This command will set the execution policy to the default setting for PowerShell.

#Setting execution policies as restricted.

#This command will display your current execution policy. This is worth noting if you wish to revert back at a later point.
Get-ExecutionPolicy

#This command will set the execution policy to the default setting for PowerShell 
Set-ExecutionPolicy Restricted

Tip: To get this PowerShell command block as a script file (PS1), click here.

See Also

PowerShell for Office 365 administrators

Office 365 PowerShell scenarios

Reporting scenarios for Office 365 PowerShell

User management scenarios for Office 365 PowerShell

More functionality scenarios for Office 365 PowerShell

Office 365 PowerShell community resources

Expand your skills
Explore training
Get new features first
Join Office Insiders

Was this information helpful?

Thank you for your feedback!

Thank you for your feedback! It sounds like it might be helpful to connect you to one of our Office support agents.

×