10 Most Useful PowerShell Commands for Office 365

Your download is in progress and it will be complete in just a few seconds! If you face any issues, download manually here

Thank you for registering with ManageEngine.

We will send the download link to the registered email ID shortly.

Manage and Secure Microsoft 365 with M365 Manager Plus

  •  
  •  
  •  
  • By clicking 'Download 30-day free trial' you agree to processing of personal data according to the Privacy Policy.

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.

  1. Download and install the Microsoft Online Services Sign-In Assistant for IT Professionals RTW.
  2. Import the Online Services PowerShell module for Microsoft Azure Active Directory and Office 365:
    1. Install-Module -Name AzureAD
    2. Install-Module -Name MSOnline
  3. Run the below command and enter your Office 365 admin credentials in the pop-up that appears.
    1. $Cred = Get-Credential
  4. Create a remote PowerShell session.
    1. $O365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication Basic -AllowRedirection
  5. Import the session commands into the local Windows PowerShell session.
    1. Import-PSSession $O365
  6. Connect to all Office 365 services.
    1. 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.

  1. Run the below script to connect to Exchange online module. When prompted enter the Office 365 admin credentials.
    1. $Cred = Get-Credential
    2. $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication Basic –AllowRedirection
  2. To connect o SharePOint Online, download and install the SharePoint Online Management Shell, and then run the below script.
    1. $admin="Admin@enterprise.onmicrosoft.com"
    2. $orgname="enterprise"
    3. $userCred = Get-Credential -UserName $admin -Message "Type the password."
    4. 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

  1. Get-Command -module MSOnline

To get the list of all Azure Active Directory cmdlets, run the below command,

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. Get-MsolGroup

To find theGroupMemberObejctId of the users you need to add or remove, run the below command.

  1. Get-MsolUser | Select ObjectID.

To add new members to a group, run the below command.

  1. 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.

  1. $GroupId = Get-MsolGroup -SearchString "MyGroup"
  2. $UserId = Get-MsolUser -UserPrincipalName "evannarvaez@contoso.com"
  3. 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,

  1. 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.

  1. Get-mailbox | get-MailboxStatistics

To retrieve the list of all the mailboxes that haven’t been logged into during the last 30 days.

  1. 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.

  1. Get-MailTrafficTopReport
 
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.

Why businesses choose M365 Manager Plus?

What our customers say

  • We can now effectively reconcile which licenses we are using in the organization and assign the cost to the business unit. We were also able to identify a number of license changes that could be put in place that reduced our total Microsoft 365 spending.

    Timothy Ransom
    Group IT/IS manager at The Eclipse Group, United Arab Emirates

  • M365 Manager Plus is valuable to our future business and, most importantly, it allows me to keep improving the level of service we provide.

    IT infrastructure manager
    Sunstar Suisse S.A.

  • Our firm purchased ManageEngine M365 Manager Plus to help us protect our business from email-borne cyberthreats. We are absolutely satisfied with the features and ease of use. It has surpassed our expectations.

    Insurance agent
    Security and risk management Gartner review

A holistic Microsoft 365 administration solution

One-stop solution for all your
Microsoft 365 administration needs!

Download now