Connect To O365 With PowerShell
Created: 8/10/2023
Last Modified: 10/24/2024
- Run Windows Powershell ISE (or “regular” Windows PowerShell) as Administrator
-
Install Microsoft Azure Active Directory Module for Windows PowerShell
-
Type the command: Install-module -name MSOnline
- If you get a message about NuGet version 2.8.5.201 or newer click yes or type Y
- If you get a message about installing the module from an untrusted repository click yes or type Y
-
Verify that the module is installed with the command: Get-Module –ListAvailable
-
Look for the MSOnline module, version 1.1.183.57 (or higher):

-
Install Azure Active Directory PowerShell for Graph
-
Type the command: Install-Module -Name AzureAD
-
Verify that the module is installed with the command: Get-Module –ListAvailable
-
Look for the AzureAD module, version 2.0.2.140 (or higher):

-
Connect to Office 365
-
Type the command: Connect-MsolService
- You will get prompted to sign in
-
Verify connection
-
Type the command: Get-MsolCompanyInformation
-
If you are connected, PowerShell will display the company info:

-
Connect to Azure Active Directory
-
Type the command: Connect-AzureAD
-
The AzureAD module will automatically display company information when it connects:

-
You can get a list of commands with the following commands:
-
O365 Module commands: Get-Module –ListAvailable
-
AzureAD Module commands: Get-Command -Module AzureAD
-
MSOnline Module commands: Get-Command -Module MSOnline
-
References:
-
How to connect:
https://activedirectorypro.com/powershell-connect-to-office-365/
-
List of commands:
https://activedirectorypro.com/powershell-commands/
-
If you need access to Get-Mailbox and other commands, you might need to update the module ExchangeOnlineManagement with the command:
Install-Module -force ExchangeOnlineManagement
Then connect using:
Connect-ExchangeOnline
Examples:
Get all Office 365 Users:
Get-MsolUser -All | select-object *
Get all licensed users:
Get-MsolUser -All | Where-Object { $_.isLicensed -eq "TRUE" }
Get all licensed users, output to local file:
Get-MsolUser -All | Where-Object { $_.isLicensed -eq "TRUE" } | Export-Csv c:\users\oddarne\desktop\contacts.csv
Get all licensed users with filters, output to local file:
Get-MsolUser -All | Where-Object { $_.IsLicensed -eq "TRUE" } | Where-Object { $_.DisplayName -notin ("Wrapper","Mat Cutter","frpsales","SkidBuilder South","Opportunities") } | Where-Object { $_.DisplayName -notlike "BRP*" } | Where-Object { $_.DisplayName -notlike "*Simmons" } | Where-Object { $_.DisplayName -notlike "Parts Puller*" } | Where-Object { $_.Department -notin ("Reinforced Logistics","OSCO Safety","OSCO") } | Export-Csv c:\users\oddarne\desktop\contacts.csv
Get Azure devices:
Get-AzureADDevice
Set a user’s password:
Set-MsolUserPassword-UserPrincipalName "<email address>"
-NewPassword "New Password"
Assign a license to a user: Set-MsolUserLicense
-UserPrincipalName "user name" -AddLicenses "<licensed name>"
Assign a rule to user’s inbox:
New-InboxRule -Mailbox oddarne -Name "BRP Admin Rule” -BodyContainsWords "You don’t often get email from", -ForwardAsAttachmentTo "emailalerts@bedfordreinforced.com" -ExceptIfSubjectContainsWords "Re:","FW:" -ExceptIfFromAddressContainsWords "@bedfordreinforced.com","@bedfordplastics.com","@oscosafety.com","@oilfieldsupplycompany.com","@reinforcedlogistics.com"
Remove user’s inbox rule:
Get-InboxRule -mailbox oddarne -Identity "First Contact Rule - Admin Use" |Remove-InboxRule -Confirm:$false
Get users and email addresses filtered by company:
BRP:
Get-MsolUser -All |
Where-Object { $_.IsLicensed -eq "TRUE" } |
Where-Object { $_.DisplayName -notin ("Training","Wrapper","Mat Cutter","frpsales","SkidBuilder South","Opportunities") } |
Where-Object { $_.DisplayName -notlike "BRP*" } |
Where-Object { $_.DisplayName -notlike "*Simmons" } |
Where-Object { $_.DisplayName -notlike "Parts Puller*" } |
Where-Object { $_.Department -notin ("Reinforced Logistics","OSCO Safety") } |
Select-Object DisplayName, SignInName, Department, Title, PhoneNumber |
Export-Csv "c:\users\oddarne\desktop\BRP Contacts.csv"
OSCO Safety:
Get-MsolUser -All | Where-Object { $_.IsLicensed -eq "TRUE" } |
Where-Object { $_.Department -eq ("OSCO Safety") } |
Select-Object DisplayName, SignInName, Department, Title, PhoneNumber |
Export-Csv "c:\users\oddarne\desktop\OSCO Contacts.csv"
Reinforced Logistics:
### Connect to O365:
#Connect-MsolService
#Connect-AzureAD
### Get users with license and filter department
Get-MsolUser -All | Where-Object { $_.IsLicensed -eq "TRUE" } |
Where-Object { $_.Department -eq ("Reinforced Logistics") } |
Select-Object @{label='First Name';expression={$_.FirstName}},
@{label='Last Name';expression={$_.LastName}},
@{label='Company';expression={"Reinforced Logistics"}},
@{label='Job Title';expression={$_.Title}},
@{label='Business Phone';expression={$_.PhoneNumber}},
@{label='E-mail Address';expression={$_.FirstName+"."+$_.LastName+"@reinforcedlogistics.com"}},
@{label='E-mail Display Name';expression={$_.DisplayName}} |
Export-Csv "c:\users\oddarne\desktop\RL Contacts.csv" -NoTypeInformation