O365-PowerShell login with saved credential

O365-PowerShell login with saved credential

Please refer to the following steps to connect to Office 365 using PowerShell

  1. Go to Office 365 DEMO and sign up for 90 Days Trial Account if you are Microsoft Partner
  2. Download Microsoft Online Services Sign-in Assistant for IT Professionals RTW-msoidcli_64.msi and install it
  3. Run “Install-Module MSOline” to install Windows Azure AD Module
  4. Prepare the saved Credential for automate login in the future
$UserName = "admin@M365x641205.onmicrosoft.com"
"XXXXX" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\scripts\Password.txt"
$Password = Get-Content "C:\Scripts\Password.txt" | ConvertTo-SecureString

#Specify Credential with password 
$Credential = new-object -typename System.Management.Automation.PSCredential -ArgumentList $UserName,$Password

#Login to Office 365
Connect-MsolService -Credential $Credential

Connect to Exchange Online
We can login to Exchange Online using

#Change the Execution Policy to RemoteSigned 
Set-ExecutionPolicy RemoteSigned -Force #First Time only 

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
-Credential $Credential -Authentication  Basic -AllowRedirection

Import-PSSession $Session

Disconnect the session once you had completed your tasks

  1. Remove-PSSession $Session to Disconnected from Exchange-Online
  2. Close the PowerShell to disconnect from msol

Reference Link
1. Connect to Office 365 PowerShell

Leave a Comment