PowerShell Profile Function to Connect to Office 365 PowerShell

To make it easy to connect to Office 365 remote PowerShell you can edit your PowerShell Profile to include a function.

First thing to do is check to see if you have a Profile created, to do this launch PowerShell and type “Test-Path $profile”, if True is returned you have a profile created, if False is returned you need to create it using this command “New-Item -path $profile -type file -force”

Now to edit your profile simply type “Notepad $profile”  A blank Text document in Notepad will appear (unless you have already configured a profile, if this is the case your existing profile will open in Notepad)

Here is the function for connecting to Office 365 Remote PowerShell:

Function Connect-O365
{
$Creds = Get-credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Creds -Authentication Basic -AllowRedirection
Import-PSSession $Session
Import-Module Msonline
Connect-MSOLService -credential $creds
}

Copy and paste the above into the Notepad window.  If you want you can add a reminder to yourself on how to use the Function.  Insert a line at the top of the profile and enter the below text

Write-Host “To connect to Office 365 with Remote Powershell type ‘Connect-O365′”

One additional note, if you do not have the Windows Azure Active Directory Module installed you will get an error as the Function calls the Module and connects to it as well.  Install the Windows Azure AD Module from here, http://technet.microsoft.com/en-us/library/jj151815.aspx#bkmk_installmodule

Now all you need to do is save the Profile, close and then re-launch PowerShell.  Simply type ‘Connect-O365’ and you will be prompted for your Office 365 credentials and connected to Office 365 Remote PowerShell and the Windows Azure AD cmdlets.

Advertisements

Leave a Reply