How to connect to Office 365 via PowerShell

*** Updated to include all Office 365 and Azure Active Directory services ***

-------------------------------------------------------------------------------------------------------

To connect to Office 365 and Azure AD via PowerShell, follow the steps below.


Pre-requisites - First you need to ensure your desktop PC is configured to connect to Office 365 via PowerShell.
Follow this tutorial to configure your desktop PC for Office 365 Administration Link

-------------------------------------------------------------------------------------------------------

Once your PC is configured, you will need to create a connection script.
*** Download and save my Office 365 Connection scriptLink - ***
Next, edit my connection script by opening the file in Notepad or PowerShell ISE.
I personally prefer PowerShell ISE as it provides visual cues when creating and editing scripts.

To edit the script in PowerShell ISE, open PowerShell ISE on the left side of your window, and have Windows Explorer open on the right. Browse to your download directory and drag the file - Connect-Office365_V2-x.ps1 into PowerShell ISE.














You will need to edit the two variables in the script to match your Office 365 tenant administrator login. Scroll down until you see the following section -
###   Edit the two variables with your details  ###










In the 'Edit the two variables section, you will see two variables to update.







Edit the first variable 
$Tenant - "TenantName" by replacing TenantName with name of your tenant.
Original script
Updated script






Edit the second variable - 
$Cred - "admin@tenant.onmicrosoft.com" by entering your admin credentials
Original Script






Updated Script




Now that you have edited the two variables with your tenant name and admin credentials, save your connection script - Connect-Office365.ps1










---------------------------------------------------------------------------------------------


One of the great features of PowerShell ISE, is that you can edit and RUN your scripts from the same program. Once you have saved your script, simply click on the green arrow to run the script -






Once you click the green arrow and run the script, a PowerShell credential box will appear. This is prompting for your password for the Office 365 admin user that you edited in the script.












Enter your password and then click OK -












A green progress bar will appear as it connects and verifies what permissions you have.
You will then have text appear similar to below.




If your connection script has run successfully, depending on your tenant configuration, you should not receive any errors.

To test and confirm successful connection to each of the nine services, run the following cmdlets.

Test - Exchange Online
Get-Mailbox

Test - Azure AD v1.0
Get-MSOLUser

Test - Azure AD v2.0 Admin
Get-AzureADuser

Test - Azure Resource Manager
Get-AzureRmContext

Test - SharePoint Admin
Get-SPOTenant

Test - Skype for Business Admin
Get-CSTenant

Test - Security and Compliance Centre
Get-HoldCompliancePolicy

Test - Exchange Online Protection
Get-TransportRule

Test - Azure Active Directory Rights Management
Get-AADRM


Congratulations -
You have now confirmed that you can successfully connect via to all Office 365 and Azure services via the Connect-Office365 script.

---------------------------------------------------------------------------

Basic PowerShell Tutorials
01. How to configure your desktop PC for Office 365 Administration - Link
02. How to connect to Office 365 via PowerShell - Link
03. How to create basic PowerShell scripts - Link
04. How to create basic PowerShell scripts with Export-CSV - Link
05. How to create basic PowerShell scripts with Import-CSV - Link

Series Tutorials -
How to manage Enterprise environments - Part 1 - Filtering queries - Link
How to manage Enterprise environments - Part 2 - Creating scripts with a filtered query - Link
How to manage Enterprise environments - Part 3 - Bulk management using multiple filters - Link

Tips and Tricks
General Tips and Tricks for better Office 365 Administration - Link
How to extend your Office 365 Trial - Link
How to get a 180 day trial tenant in Office 365 for testing - Link

------------------------------------------------------------------------------

2 comments:

  1. Hi Terry,
    Great script and enables me to access O365 still using ISE which is much preferred. After running it and completing authentication (MFA enabled) ISE stops responding after a period of time. Have googled it and seems to be a Forms loading issue with ISE. Have you experienced this? Or do you have any workarounds to avoid this?

    ReplyDelete
  2. Thanks Rob, yes, I have experienced with this issue and confirmed it in multiple tenants as well as a colleague confirm as well.
    I am like you and prefer to create and run scripts from within ISE. I have updated the Exchange Online PowerShell by re-downloading with no resolution.
    At this stage, if you want a reliable experience, it looks like you will need to create your scripts in ISE and run them in the dedicated PowerShell. Not a great experience.
    If you do get a work around, please let me know and I will advise my connections via LinkedIn - https://www.linkedin.com/in/terry-munro/

    ReplyDelete