PowerShell is easily my favorite tool Microsoft provides system administrators with. I love automation because I am lazy and dont want to have to do simple tasks manually through a GUI. This is the shared feeling by most users of PowerShell.
One of the great things about PowerShell is its integration with the entire Microsoft Ecosystem. Microsoft themselves provide modules for nearly all their technologies, and Office 365 is one of them. As I’ve had a particular focus on the Microsoft cloud offerings, this is incredibly important to me.
The Commands To Connect to the Cloud
First, the prerequisites:
- You need to install the Microsoft.NET Framework 4.5
- You need to install the Microsoft Windows Management Framework 3.0 or 4.0
- You need to be able to run remote scripts.
- As an Administrator, open powershell and run “Set-ExecutionPolicy RemoteSigned”
- You need to Install the Microsoft Online Services Sign-In Assistant
- You need to install the Windows Azure Active Directory Module (64-bit)
Lots of things to install, but if you are constantly in Office 365, it is an easy thing to install them once and be done. Now, there are just three lines you have to run in order to connect!
1 2 3 |
$cred = Get-Credential Import-Module MsOnline Connect-MsolService -Credential $credential |
Boom, connected to your Office 365 Azure Active Directory. Young naive me would copy each of those three lines individually each time I wanted to make a connection to a particular Azure Active Directory (which I was doing frequently). So, I needed to make it easier.
PowerShell Profile!
I just needed to make a function out of those three lines, and with one simple command, I was connected to Azure Active Directory in Office 365. So, I setup my PowerShell Profile and made a function out of it.
1 2 3 4 5 |
function Connect-AAD { $cred = Get-Credential Import-Module MsOnline Connect-MsolService -Credential $credential } |
Simple, my single command would ask for my credentials and would connect automatically for me.
One Step Further for Exchange
However, I would primarily connect to Office 365 through PowerShell for Exchange related tasks, so I wanted to make that connection at the same time. So, I edited my function (and renamed it) so it would make both those connections in the same session.
1 2 3 4 5 6 7 |
function Connect-O365 { $cred = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $cred -Authentication Basic -AllowRedirection Import-PSSession $Session Import-Module MsOnline Connect-MsolService -Credential $cred } |
And that is it! I was able to run my single command and have the functionality I needed. You could continue this progression with add the commands for connecting to SharePoint Online and Skype for Business Online, but I was content with Exchange and Azure AD.