PowerShell still remain one of the most valuable tools for Office 365 administrators. It comes in handy when they need to retrieve a specific list of users or mailboxes. Instead of rummaging through the options available in the Office 365 portal, administrators find it easy to key-in a few lines of code to get what they need. In this article we will learn about the ten most commonly used PowerShell cmdlets.
1. Connect to an Office 365 instance using PowerShell
Following are the steps to connect to the Office 365 instance. Each step mentions the function to performed and respective PowerShell script to do the same.
- Download and install the Microsoft Online Services Sign-In Assistant for IT Professionals RTW.
- Import the Online Services PowerShell module for Microsoft Azure Active Directory and Office 365:
- Install-Module -Name AzureAD
- Install-Module -Name MSOnline
- Run the below command and enter your Office 365 admin credentials in the pop-up that appears.
- Create a remote PowerShell session.
- $O365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication Basic -AllowRedirection
- Import the session commands into the local Windows PowerShell session.
- Connect to all Office 365 services.
- Connect-MsolService –Credential $O365
2. Connect to Exchange Online and SharePoint Online using PowerShell
Following are the steps to connect Exchange Online and SharePoint Online with PowerShell.
- Run the below script to connect to Exchange online module. When prompted enter the Office 365 admin credentials.
- $Cred = Get-Credential
- $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication Basic –AllowRedirection
- To connect o SharePOint Online, download and install the SharePoint Online Management Shell, and then run the below script.
- $admin="Admin@enterprise.onmicrosoft.com"
- $orgname="enterprise"
- $userCred = Get-Credential -UserName $admin -Message "Type the password."
- Connect-SPOService -Url https://$orgname-admin.sharepoint.com -Credential $userCred
Please provide a valid admin email address and enterprise name in the first two lines of the script.
3. Get a list of available Office 365 PowerShell cmdlets
To get the list of all MSOnline PowerShell commands, run the below command
- Get-Command -module MSOnline
To get the list of all Azure Active Directory cmdlets, run the below command,
- Get-Command -module AzureAD
4. Get the list of all Office 365 users with PowerShell
To retrieve all users with a valid license in the Office 365 tenant, along with the DisplayName, City, Department and ObjectID parameters, run the below command.
- Get-MsolUser | Select DisplayName, City, Department, ObjectID
5. Create a new user in Office 365 using PowerShell
To create a new Office 365 user with first name John and last name Smith, run the below command.
- New-MsolUser -UserPrincipalName JSmith@enterprise.onmicrosoft.com -DisplayName "John Smith" -FirstName “John” -LastName “Smith”
If the Office 365 user creation was success, the system will return the user's password and license status.
6. Remove a user from all SharePoint sites using PowerShell
To remove a user from all the SharePoint sites at once, run the below command.
- Get-SPOSite | ForEach {Remove-SPOUser -Site $_.Url -LoginName " JSmith@enterprise.onmicrosoft.com"}
7. Change Office 365 user account password using PowerShell
If you need to change the password for an account, use the Set-MsolUserPassword cmdlet.
- Set-MsolUserPassword -UserPrincipalName JSmith@netwrixqcspa.onmicrosoft.com -NewPassword P@SSw0rd!
You can leave out the -NewPassword parameter to allow the system to generate a random password.
8. Manage group membership in Office 365 using PowerShell
To manage a group, you need to know the GroupObjectId and GroupMemberObejctId.
To find the groupObjectID of the group you need to manage, run the below command.
To find theGroupMemberObejctId of the users you need to add or remove, run the below command.
- Get-MsolUser | Select ObjectID.
To add new members to a group, run the below command.
- Add-MsolGroupMember -GroupObjectId 5b61d9e1-a13f-4a2d-b5ba-773cebc08eec -GroupMemberObjectId a56cae92-a8b9-4fd0-acfc-6773a5c1c767 -GroupMembertype user
To remove a user from a group, run the below command.
- $GroupId = Get-MsolGroup -SearchString "MyGroup"
- $UserId = Get-MsolUser -UserPrincipalName "evannarvaez@contoso.com"
- Remove-MsoLGroupMember -GroupObjectId $GroupId -GroupMemberType User -GroupmemberObjectId $UserId
This example removes the user evannarvaez@contoso.com from the group named MyGroup.
9. Create a SharePoint site collection using PowerShell
To create a SharePoint site collection with PowerShell, run the below command,
- New-SPOSite -Url https://contoso.sharepoint.com/sites/mynewsite -Owner john.smith@contoso.com -StorageQuota 1000 -Title "Latest presentation"
This example creates a new site collection in the name Latest presentation for the current company with specified site URL, and owner. The storage quota is set to 1000 megabytes.
10. Create reports in Office 365 using PowerShell
PowerShell is a great tool for making different reports. Here are some useful Office 365 reports done via PowerShell:
To generate the All Mailboxes report which provides details about all the mailboxes in your Office 365 setup.
- Get-mailbox | get-MailboxStatistics
To retrieve the list of all the mailboxes that haven’t been logged into during the last 30 days.
- Get-Mailbox –RecipientType 'UserMailbox' | Get-MailboxStatistics | Sort-Object LastLogonTime | Where {$_.LastLogonTime –lt ([DateTime]::Now).AddDays(-30) } | Format-Table DisplayName, LastLogonTime
To get a summary of the mail traffic in your Office 365.
Note: Most of the reporting cmdlets were deprecated in January 2018 and replaced by the new MS Graph API. Therefore, some reports are now available only in the Office 365 Security & Compliance Center.