Connect Powershell to Exchange Online

Check for connection, if not, then Establish connection

# Check for connection
$NotConnected = $true
$RMsessions = Get-PSSession
Foreach ($RMsession in $RMsessions) {
if ($RMsession.Computername -like "*outlook*") { $NotConnected = $false }
}

# Establish connection
if ($NotConnected ) {
$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session 
cls
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.