Quantcast
Channel: Tachytelic.net
Viewing all articles
Browse latest Browse all 210

How to connect to Office 365 with PowerShell

$
0
0

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

  1. Open a PowerShell session
  2. Store your Credentials in a variable:
    $Cred = Get-Credential
  3. Enter your Office 365 Credentials when prompted:
    Image showing dialog of Office 365 Credentials used to connect to Office 365
  4. 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

    Image showing import of PowerShell session from Office 365

  5. 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.

  6. Now you can run any commands you need.
  7. 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:

  1. Open PowerShell as an administrator:
    Image showing how to run PowerShell as an administrator to change execution policy
  2. In your PowerShell window run the command:
    Set-ExecutionPolicy RemoteSigned
  3. 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.


Viewing all articles
Browse latest Browse all 210

Trending Articles