In order to manage Office 365 via Remote PowerShell, there are a number of commands required. For anyone finding themselves jumping in and out of a remote powershell session, or moving between multiple tenants, continuously going through the authentication commands can be tedious. Upon opening a standard PowerShell command prompt via Windows 7, the following commands are run to authenticate:
$cred = get-credential
$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $cred -Authentication Basic -AllowRedirection
$importresults = Import-PSSession $s
import-module msonline
Rather than typing this in to a PowerShell command prompt every time I want to connect, I instead added all of these commands in Notepad and saved the file as a .ps1. Additionally, I’ve set my Execution Policy to allow unrestricted scripts (set-executionpolicy unrestricted). Now, when I want to connect, all I have to do is drag and drop my MSOnline.ps1 file in to PowerShell, fill in the appropriate Office 365 administrator credentials, and I’m logged in and ready to go.
Much easier!
« Quest MessageStats: Own Your Exchange Environment Lync-to-Phone Is Now Available in Office 365! »




Script works great, thank you!
Hello
The script is great. I have a question. How long can a remote powershell session stay connected? We have an automated account provisioning process in house and are using ADFS. I want to set an automated task to run every hour to license any unlicensed users. Can I connect a remote powershell session to office 365 and assume that it will stay connected say for one week? ..and then plan to reboot the server once a week and reconnect the session.
15 minute time limit before re-authentication is required. You can still run automated tasks, you’ll just want to make sure you include a username/password authentication process within the script. Do not assume the session will stay connected for more than 15 minutes, though. Good luck!