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

Tachytelic.net

  • Sco Openserver
    • Sco Unix Support
    • SCO Openserver Installation ISOs
    • SCO Openserver Binaries
    • Add a Hard Drive to an Openserver 5 Virtual Machine
    • Install SCO Vision SQL-Retriever ODBC Driver on Windows 10
    • License Expired on Virtual SCO Openserver Installation
    • How to reset the root password on SCO Openserver 5
  • Scripting
    • PowerShell
      • Add leading zeros
      • Check if a File Exists
      • Grep with Powershell
      • Create Environment Variables
      • Test for open Ports
      • Append to a Text File
    • VBScript
      • Check if a File Exists
      • lpad and rpad functions
      • Windows Update E-Mail Notification
  • Office 365
    • Connect to Office 365 with PowerShell
    • Add or remove an email alias using Powershell
    • Change Primary email address of Active Directory user
    • How to hide an AD user from the Global Address List
    • How to hide mail contacts from the Global Address List
    • Change the primary email address for an account with PowerShell
    • Change Primary email address of an AD User
    • Grant a single user access to access to all calendars
    • Forward email to an external address using Powershell
    • Convert shared mailbox to user mailbox with Powershell
  • Get in Touch
  • About Me
    • Privacy Policy

How to connect to Office 365 with PowerShell

December 22, 2018 by Paulie 2 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.

Related

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!!

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
Go to mobile version