• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Tachytelic.net

  • Get in Touch
  • About Me

How to connect to Office 365 with PowerShell

December 22, 2018 by Paulie 3 Comments

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 times when PowerShell is more suited. 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.

Filed Under: How To Tagged With: Office 365, Powershell

Reader Interactions

Comments

  1. Jonah says

    September 3, 2020 at 5:28 pm

    Vary helpful, thank you very much!!

  2. mainwebsolutions says

    April 25, 2022 at 2:00 pm

    Hi there, it seems like it doesn’t work anymore. I tried a few month ago and it worked. but since i tried today it doesent. Can you check that and inform me? The error is happening here:
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection

    WARNUNG: Ihre Verbindung wurde an den folgenden URI umgeleitet:
    “https://ps.outlook.com/PowerShell-LiveID?PSVersion=5.1.19041.1645”
    New-PSSession : [ps.outlook.com] Beim Verbinden mit dem Remoteserver “ps.outlook.com” ist folgender Fehler
    aufgetreten: Zugriff verweigert Weitere Informationen finden Sie im Hilfethema “about_Remote_Troubleshooting”.
    In Zeile:1 Zeichen:12
    + $Session = New-PSSession -ConfigurationName Microsoft.Exchange -Conne …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : OpenError: (System.Manageme….RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotin
    gTransportException
    + FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed

    I can login to office admin account so the credentials should be right. And the user is also a global admin. Does anyone know a solution?

Trackbacks

  1. Office 365:Convert shared mailbox to user mailbox with Powershell says:
    December 23, 2018 at 2:33 pm

    […] to set your PowerShell execution policy, otherwise the script will not run. Set the PowerShell execution policy to at least […]

Leave a Reply Cancel reply

Primary Sidebar

Link to my LinkedIn Profile
Buy me a coffee

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 245 other subscribers.

Go to mobile version