Connecting to Exchange Online PS Session

The code below allows you to connect to Microsoft’s Office 365 Exchange Powershell. The script itself can be downloaded from this Github link.

#THIS BLOCK WILL ASK FOR DOMAIN USERNAME OF O365 ADMIN AND PASSWORD
#THIS APPROACH WAS INTENTIONAL SINCE IT IS NOT SECURE TO STORE ADMIN PASSWORDS IN FILES
Param (
    [Parameter(Mandatory=$True)]
    [string]$username_with_domain,
    [Parameter(Mandatory=$True)]
    [string]$password
    )

#CONVERTS THE SUPPLIED PASSWORD INTO A SECURE STRING
$SecPaswd= ConvertTo-SecureString –String $($password) –AsPlainText –Force

#SETS THE CREDENTIAL TO BE SUPPLIED WHEN LOGGING IN TO EXCHANGE ONLINE
#THIS ELIMINATES THE POP-UP ASKING FOR CREDENTIALS
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $($username_with_domain), $SecPaswd

#CREATES THE 0365 POWERSHELL SESSION
$mySession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection

#IMPORTS THE O365 POWERSHELL SESSION TO EXISTING PS SESSIONS
Import-PSSession $mySession