Automating a PowerShell Connection to Office 365
This blog post will help you create a desktop shortcut to automatically connect you to Office 365 using PowerShell. We promise that this will help you save time and minimize frustration caused by mis-typed PowerShell commands.
Firstly, copy & paste in the following two commands into your favourite text editor (Such as Notepad)
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $cred -Authentication Basic -AllowRedirection
Save the file as Connect-PS.ps1 in a folder such as c:\MyScripts\
Make sure the file extension is ps1 otherwise it won’t be recognized as a PowerShell script.
Now, create a new shortcut on your desktop.
When you specify the file location for your new shortcut you will need to include 3 elements.
- Powershell.exe – This is the Windows Powershell executable
- -noexit – This tell the Powershell console to remain open after executing your new script. If you omit this, the window will close as soon as your script has completed and your session to Office 365 will be lost
- c:\MyScripts\Connect-PS.ps1 – Make sure you include the full path and name of the script you wish to run. If the path contains and spaces then you will have to include quotes. You should also include an ampersand before the path to tell Powershell you want to to execute this value. Something like this.
powershell.exe -noexit & “C:\My Scripts\Connect-PS.ps1”
Once you have saved your new shortcut to your desktop, double-click and away you go.
If you get an error when attempting to run the PowerShell script, check out this blog post about allowing PowerShell scripts to execute.