Let’s say you’re an admin that needs to connect to Office365 via PowerShell often. Now, there are many different websites or blogs that will show you how to connect to each session via PowerShell. That can cause a headache since you can end up having five different PowerShell sessions running in five different windows. You end up having to enter a username and password all those times, which can become time consuming.
I want to show you here how to combine all those sessions into one script where, if you’re security is tight enough on your computer, you don’t even have to enter credentials. This way, you can click on one icon and pull up all the O365 PowerShell commands that you’ll need to manage your organization.
First you need to download the following PowerShell Module Installation Files so that your PowerShell Database will have the correct modules installed:
Microsoft Online Service Sign-in Assistant for IT Professionals RTW
Windows Azure Active Directory Module for Windows PowerShell v2
SharePoint Online Management Shell
Skype for Business Online, Windows PowerShell Module
Next, we want to setup the CLI (Command Line Interface) to be too cool for school. I have learned it helps to have knowledge of how to customize the CLI window. You can do all of this in PowerShell ISE or Notepad, which ever you prefer. Here are the commands for the script that I use to setup the CLI:
1 2 3 4 5 6 7 8 9 | #Set the PowerShell CLI. Make sure you have a directory named "C:\PowerShell" set-location c:\PowerShell $a = (Get-Host).UI.RawUI $a.BackgroundColor = "black" $a.ForegroundColor = "yellow" $a.WindowTitle = "(Your Company Name) PowerShell for ALL O365 PowerShell Services" $curUser= (Get-ChildItem Env:\USERNAME).Value function prompt {"(Your Company Name) O365 PS: $(get-date -f "hh:mm:ss tt")>"} $host.UI.RawUI.WindowTitle = "(Your Comapny Name) O365 PowerShell >> User: $curUser >> Current Directory: $((Get-Location).Path)" |
Next, you want to set your Execution Policy and put in your credentials so that you won’t be prompted to enter the user credentials when you run the script.
NOTE: MAKE SURE YOU KEEP YOUR SCRIPT SAFE AS THE CREDENTIALS ARE VISIBLE WITHIN THE SCRIPT IN PLAIN TEXT!
You can, alternatively, set your script to prompt for credentials every time by using the following:
$LiveCred = Get-Credential
Here is that part of the script:
1 2 3 4 5 6 | #Setup Execution Policy and Credentials using your Tenant Admin Credentials Set-ExecutionPolicy Unrestricted $user = "tenantadmin@companyname.onmicrosoft.com" $pass = "adminpassword" $secpass = $pass | ConvertTo-SecureString -AsPlainText -Force $LiveCred = New-Object System.Management.Automation.PSCredential -ArgumentList $user, $secpass |
Now we get into the importing of the modules for each O365 service:
Get the MSOnline Module:
1 2 3 | #Set up the powershell cmdlets for Office365 Write-Host "Getting MSOnline Module" -ForegroundColor Green Get-Module MSOnline |
Connect to the MSOnline Service:
1 2 3 | #Connect the MS Online Service Write-Host "Connecting to the MSOnline Service" -ForegroundColor Green Connect-MSOLService -Credential $LiveCred |
Connect to Azure AD PowerShell:
1 2 3 | #Connect to Azure Ad PowerShell Write-Host "Connecting to Azure AD PowerShell" -ForegroundColor Green Connect-AzureAD -Credential $LiveCred |
Connect to SharePoint Online PowerShell:
NOTE – MAKE SURE YOU CHANGE TO YOUR COMPANY NAME IN THE URL!!
1 2 3 4 | #Connect to SharePoint Online PowerShell Write-Host "Connecting to SharePoint Online through PowerShell" -ForegroundColor Green Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking Connect-SPOService -Url https://companyname-admin.sharepoint.com -credential $LiveCred |
Connect to Exchange Online PowerShell:
1 2 3 4 | #Connect to Exchange Powershell Write-Host "Connecting to Exchange Online through PowerShell" -ForegroundColor Green $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection Import-PSSession $Session |
Connect to Skype For Business Online PowerShell:
1 2 3 4 5 | #Connect the Skype For Business Online Powershell Module Write-Host "Connecting to Skype For Business Online through PowerShell" -ForegroundColor Green Import-Module SkypeOnlineConnector $sfboSession = New-CsOnlineSession -Credential $LiveCred Import-PSSession $sfboSession |
Connect to the Security & Compliance PowerShell:
NOTE – This one I still get “Access Denied” when trying to connect. I have looked for an answer to that issue, but have not found one. Please comment with a link if you have an answer so that I can update this script!
1 2 3 4 | #Connect the Security & Compliance Center PowerShell Module Write-Host "Connecting to O365 Security & Compliance Online through PowerShell"-ForegroundColor Green $SccSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid -Credential $LiveCred -Verbose -Authentication Basic -AllowRedirection Import-PSSession $SccSession -Prefix cc |
Lastly, put in a note to show that the PS load is completed:
1 2 3 | Write-Host "Be sure to check for any connectivity errors!" -ForegroundColor Green Write-Host "Also, Remember to run 'Get-PSSession | Remove-PSSession' before closing your PowerShell Window!" -ForegroundColor Green Write-Host "Successfully connected to all O365 PowerShell Services for (CompanyName)!" -ForegroundColor Green |
So Here is the final script in its entirety:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #Set the PowerShell CLI. Make sure you have a directory named "C:\PowerShell" set-location c:\PowerShell $a = (Get-Host).UI.RawUI $a.BackgroundColor = "black" $a.ForegroundColor = "yellow" $a.WindowTitle = "(Your Company Name) PowerShell for ALL O365 PowerShell Services" $curUser= (Get-ChildItem Env:\USERNAME).Value function prompt {"(Your Company Name) O365 PS: $(get-date -f "hh:mm:ss tt")>"} $host.UI.RawUI.WindowTitle = "(Your Company Name) O365 PowerShell >> User: $curUser >> Current Directory: $((Get-Location).Path)" #Setup Execution Policy and Credentials using your Tenant Admin Credentials Set-ExecutionPolicy Unrestricted $user = "tenantadmin@companyname.onmicrosoft.com" $pass = "adminpassword" $secpass = $pass | ConvertTo-SecureString -AsPlainText -Force $LiveCred = New-Object System.Management.Automation.PSCredential -ArgumentList $user, $secpass #Set up the powershell cmdlets for Office365 Write-Host "Getting MSOnline Module" -ForegroundColor Green Get-Module MSOnline #Connect the MS Online Service Write-Host "Connecting to the MSOnline Service" -ForegroundColor Green Connect-MSOLService -Credential $LiveCred #Connect to Azure Ad PowerShell Write-Host "Connecting to Azure AD PowerShell" -ForegroundColor Green Connect-AzureAD -Credential $LiveCred #Connect to SharePoint Online PowerShell Write-Host "Connecting to SharePoint Online through PowerShell" -ForegroundColor Green Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking Connect-SPOService -Url https://companyname-admin.sharepoint.com -credential $LiveCred #Connect to Exchange Powershell Write-Host "Connecting to Exchange Online through PowerShell" -ForegroundColor Green $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection Import-PSSession $Session #Connect the Skype For Business Online Powershell Module Write-Host "Connecting to Skype For Business Online through PowerShell" -ForegroundColor Green Import-Module SkypeOnlineConnector $sfboSession = New-CsOnlineSession -Credential $LiveCred Import-PSSession $sfboSession #Connect the Security & Compliance Center PowerShell Module Write-Host "Connecting to O365 Security & Compliance Online through PowerShell"-ForegroundColor Green $SccSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid -Credential $LiveCred -Verbose -Authentication Basic -AllowRedirection Import-PSSession $SccSession -Prefix cc Write-Host "Be sure to check for any connectivity errors!" -ForegroundColor Green Write-Host "Also, Remember to run 'Get-PSSession | Remove-PSSession' before closing your PowerShell Window!" -ForegroundColor Green Write-Host "Successfully connected to all O365 PowerShell Services for (CompanyName)!" -ForegroundColor Green |
Now you can create your icon for your desktop so that you can easily access the script. I would save the script to your Scripts directory.
That will usually be C:\Users\’username’\Documents\WindowsPowerShell\Scripts or wherever directory you choose.
To start, right click the desktop and choose New > Shortcut
In the Target Field, enter the following for your PowerShell Shortcut, pointing to the path of your script:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -ExecutionPolicy Unrestricted -File “C:\Users\username\Documents\WindowsPowerShell\Scripts\ConnectO365All.ps1”
Click on the Advanced button and check the box: Run As Administrator
Under the General Tab, name your shortcut: (CompanyName) O365 All PowerShell
Click OK to save the shortcut to your desktop.
LAST BUT NOT LEAST, RUN THE FOLLOWING COMMAND BEFORE EXITING OR CLOSING YOUR POWERSHELL WINDOW. THIS WILL REMOVE ALL THE SESSIONS YOU’VE CONNECTED TO:
Get-PSSession | Remove-PSSession
HAPPY SCRIPTING!
LEARN, DO, LIVE!
References:
Connect to all O365 Services in one PowerShell Window
How to connect to all O365 Services through PowerShell
Connecting to Office 365 “Everything” via PowerShell