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

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

No comments:

Post a Comment