Many of the posts on this blog and others will require you to connect to Office 365 with PowerShell. The web admin interface for Office 365 is very good, but there are just some things that cannot be done via the web interface. So this post shows you how it is done and the problems you might come up against. I will go through each step in detail and then post complete code.
Connect to Office 365 with PowerShell
- Open a PowerShell session
- Store your Credentials in a variable:
$Cred = Get-Credential
- Enter your Office 365 Credentials when prompted:
- Create a new PowerShell session from the Office 365 Server:
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection
- Import the session:
Import-PSSession $Session
If you do not receive any errors on this step, continue to step 6. You may receive the error:
Import-PSSession : Files cannot be loaded because running scripts is disabled on this system.
If you do get this error, you need to change your PowerShell Execution Policy.
- Now you can run any commands you need.
- When you have finished, remove the session you created in step 2:
Remove-PSSession $Session
Complete code to connect to Office 365 with PowerShell
$Cred = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection Import-PSSession $Session # Run any commands you want here Remove-PSSession $Session
Change PowerShell Execution Policy
If you received this error when you tried to execute step 5:
Import-PSSession : Files cannot be loaded because running scripts is disabled on this system.
Then you need to change your PowerShell execution policy. To connect to Office 365 with Powershell your execution policy to at least Remote signed:
- Open PowerShell as an administrator:
- In your PowerShell window run the command:
Set-ExecutionPolicy RemoteSigned
- Choose “Y” when prompted to change the execution policy.
You will now be able to connect to Office 365 with Powershell.
The post How to connect to Office 365 with PowerShell appeared first on Tachytelic.net.