Automate PowerShell login for Exchange 2010/13 On- Premise and O365

You can automate PowerShell login for your local and online exchange environment so you don’t have to type user name and password every time you wants to connect to Exchange PowerShell

This scrip is also helpful if you want to run some script to generate some daily reports against your exchange server from a non-exchange server.

There are multiple methods to store your password to use in a script.

I will cover Clear password and xml file method to automate login process.

In Clear password method you store password In a Powershell script in clear text and who so ever has access to that script can see the password in use for login.

In Xml file method password will be saved in encrypted form.

So first I will cover how to use the clear password method.

Below method can be used to store credential in Clear text format


$UserAccount = "Domain\UserName"

$Password = "Password"

$cred = new-object –TypeName System.Management.Automation.PSCredential –ArgumentList $UserAccount, (ConvertTo-SecureString $Password –AsPlainText –Force)

# We can use Kerberos authentication instead of basic if we does not have certificate installed from trusted authority, limitation are that you can only connect to exchange powershell from the domain joined system.

$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $cred -Authentication Kerberos –AllowRedirection

Import-PSSession $s -AllowClobber





Now I will show you the 2nd method for login Automation.

So let’s First Export Credential to an Xml file and then we will use the same file for authentication in our Script Later.

# Use the below code in Script to Export Credentials to file

$cred = Get-Credential

#Edit below file location to where you what the credential file to be stored

$CredFile = "”FileLocation”\CredFile.xml"

$CredToFile = $cred | Select-Object *

$CredToFile.password = $CredToFile.Password | ConvertFrom-SecureString

$CredToFile | Export-Clixml $CredFile

Once the Credentials are exported to CredFile.xml we can use them to login in our login script

Use the below code in login scrip to login automatically it will use credential from the file CredFile.xml



#change the path to the credfile.xml file below

$CredFile = "”FileLocation”\CredFile.xml"

$CredFromFile = Import-Clixml $CredFile

$CredFromFile.password = $CredFromFile.Password | ConvertTo-SecureString

$Cred=New-Object system.Management.Automation.PSCredential($CredFromFile.username, $CredFromFile.password)

#change the server name & authentication type accordingly

$s=New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $cred -Authentication kerberos -AllowRedirection

Import-PSSession $s

If you want to connect to exchange online O365 you can change the server address to and Authentication type as Basic and use the same method to automate the login process.

I hope this article helps you to automate login process for your scrip; please comments with your feedback and suggestions.