Manage Microsoft 365 with PowerShell to save time

Managem365wpowershell Featured

Learn the basics of using PowerShell for managing Microsoft 365, along with four of our favorite commands you should have in your back pocket.

Simplifying menial tasks when using Microsoft 365 products and services is something every IT admin thinks about. Everyone hopes for simple ways to automate tasks, reports, and configuration management. 

Enter PowerShell. A former Windows component made open-source, PowerShell is a program that uses a command-line shell to write scripts and automate tasks in Microsoft 365.  

In this article, we’ll go over the basics of using PowerShell for managing Microsoft 365, along with four of our favorite commands you should have in your back pocket. Let’s get to it! 


Why use Microsoft 365 with Powershell?

For a long time, IT admins were stuck with the hassle of executing simple commands using the standard Windows command line utility. And even then, there was a sense of limitation in what you could achieve with this method.  

Many IT admins had to resort to writing console applications to get the job done. And despite all this, scripting limitations still got in the way of the intended outcome.  

And then PowerShell came to the rescue. With it, administrators could achieve more by automating many menial tasks. Some powerful features of PowerShell include: 

  • Automation of tasks using scripts 
  • Creation of shareable commands i.e. Powershell cmdlets 
  • Capacity to interact with different data formats, including XML, JSON, and CSV  
  • More uniform task management across environments 
  • Integrated Scripting Environment (ISE), a Graphical User Interface (GUI) to write, test, and debug scripts 
  • Solid community support and documentation to guide users

Thankfully, we have several PowerShell script examples that can help you automate your Microsoft administration tasks.


Manage SharePoint Online with PowerShell

PowerShell has a ‘SharePoint Online Management Shell’ that comes pre-equipped with SharePoint libraries that can be used to perform common activities.  

Now, you’ll want to take note of the stark difference between SharePoint Online PowerShell commands and Microsoft 365 PowerShell commands. In SharePoint Online, PowerShell commands start with SPO as its noun, while in Microsoft 365, they start with MSO.  

Also, Microsoft provides you with a handy tool called Powershell Command Builder that enables you to create commands by picking up on verbs and nouns actions of the task. Simply by assigning a property and using a ‘copy to clipboard’ action, the available commands can be used in PowerShell.  


Connect Powershell to Microsoft 365

PowerShell can be used to manage Microsoft 365 / Office 365 directly from the command line. But before we get into how to connect Microsoft 365 with PowerShell, you should know that Microsoft is transitioning from Azure Active Directory Module to Microsoft Graph PowerShell SDK. So far, the transition isn’t complete, and the Azure Active Directory Module is still being used. But you shouldn’t worry about it, since both versions work in most cases and can safely be installed on the same computer. 

Some requirements before you start

To connect to Microsoft 365 using PowerShell, the following requirements must be met: 

  • 64-bit version of Windows must be used 
  • Windows 10, Windows 8.1, Windows 8, or Windows 7 Service Pack (SP1) must be used 
  • PowerShell version 5.1 must be used for Azure Active Directory PowerShell for Graph module 
  • Only Powershell version 5.1 to 6 can be used for Microsoft Azure Active Directory Module for Windows PowerShell module 

Steps to connect to Microsoft 365 using PowerShell

Connecting with the Azure Activity Directory PowerShell for Graph module

Step 1: Installing the software

Open the PowerShell command prompt window and type the following command: 

Install-Module -Name AzureAD 

Run the command, and press Yes/Yes to All to proceed.

To import the module, run the following command: 

Import-Module  AzureAD 

Step 2: Connecting to Azure AD 

There are multiple ways to connect to Azure AD depending on your Microsoft 365 subscription. The commands include: 

Microsoft 365 cloud Command
Microsoft 365 Worldwide (+GCC) Connect-AzureAD 
Microsoft 365 operated by 21 Vianet Connect-AzureAD -AzureEnvironmentName AzureChinaCloud 
Microsoft 365 Germany Connect-AzureAD -AzureEnvironmentName AzureGermanyCloud 
Microsoft 365 U.S. Government DoD and Office 365 U.S. Government GCC High Connect-AzureAD -AzureEnvironmentName AzureUSGovernment 

Connecting with the Azure Active Directory Module for Windows PowerShell 

Step 1: Installing the software

Open the Windows PowerShell command prompt and run the following command: 

Install-Module MSOnline 

If you’re prompted to install the NuGet provider or PSGallery module, type Y and press Enter to continue.  

Step 2: Connect to Azure AD 

There are multiple ways to connect to Azure AD depending on your Microsoft 365 subscription. The commands include: 

Microsoft 365 cloud Command
Microsoft 365 Worldwide (+GCC) Connect-MsolService 
Microsoft 365 operated by 21 Vianet Connect-MsolService -AzureEnvironment AzureChinaCloud 
Microsoft 365 Germany Connect-MsolService -AzureEnvironment AzureGermanyCloud 
Microsoft 365 U.S. Government DoD and Office 365 U.S. Government GCC High Connect-MsolService -AzureEnvironment USGovernment 

Step 3: Sign in 

In the ‘Sign in to your account dialogue box’, enter your Microsoft 365 credentials and select Ok.  


PowerShell tips and tricks for Microsoft 365

There are a lot of handy commands available in PowerShell for managing Microsoft 365. Here are some tips for pulling off some neat things.

Tip #1: Get a list of users and create a new Microsoft 365 user with PowerShell 

You can get a list of users in Microsoft 365 with a valid license using the command: 

Image 2

Once executed, it’ll display a list of all Microsoft 365 users with their display name, city, and department. Of course, the command can be altered to include other information as well.

Additionally, you can use the New-MsolUser command to create new users in Microsoft 365 using PowerShell. To do this, you can use the following command:

Image 3

Not only can you create new users, but you can create users in bulk by using certain PowerShell commands. This powerful capability is also one of the advantages that reflects the power of automating tasks using PowerShell.  

Tip #2: Managing Microsoft 365 passwords with PowerShell 

In addition to creating Microsoft 365 users with PowerShell, you also have advanced capabilities for managing passwords. For example, let’s say Tim Draper (the user created in the previous section) needs to be assigned a password to access his new account. The IT admin will e-mail him a temporary password. Tim will open the e-mail and be asked to create his new custom password.  

Additionally, to change an existing password in M365 using PowerShell, you can use the Set-MsolUserPassword command.  

Set-MsolUserPassword -UserPrincipalName "[email protected]"  
ForceChangePassword 0 -NewPassword "Superpowershell" 

Note: The ForceChangePassword to false (0) command has been used to remove the restriction of being forced to change the password.

Tip #3: Manage Microsoft 365 groups with PowerShell 

PowerShell allows advanced capabilities for managing Microsoft 365 groups. Some of these capabilities include: 

  • Retrieving list of all Microsoft 365 groups: The Get-MsolGroup command can be used to retrieve a list of all groups in Microsoft 365.  
  • Adding new users to a Microsoft 365 group: The Add-MsolGroupMember command can be used to add new users.  
Add-MsolGroupMember -GroupObjectId <groupobjectid> -GroupMemberObjectId - <groupmemberobjectid> -GroupMembertype user

Note: The GroupObject ID of the created group can be retrieved using the GetMsolGroup command. The user object id can be retrieved using the GroupMemberObejctId command. 

Tip #4: Manage SharePoint sites with PowerShell 

Finally, when we have the required users and groups, we can go ahead and create SharePoint site using PowerShell. To do this, use the following commands: 

  1. New-SPOSite -Url “https://sharepointarena.sharepoint.com/sites/TeamSite01” -Owner “[email protected]” -StorageQuota “100” -Template ” EHS#1 
  2. -Title “Roberts Team Site” 

Save time and money with Microsoft 365 automation

Leveraging PowerShell for Microsoft 365 is a smart approach towards automating tasks and saving your organization time and resources.

This is even more crucial for IT teams who are constantly in a reactive mode trying to keep up with all the manual work they’re involved with. Using PowerShell for Microsoft 365 provides a way out for IT teams shifting towards a proactive approach.

However, finding or creating the right scripts in PowerShell can be time-consuming, and it might require a skillset that not everyone on your IT team has.  

If you’re looking for a simpler, more efficient alternative that provides out-of-the-box automation for your Microsoft 365 environments, ShareGate is the answer. These are some of the things you can use ShareGate for, without the need for PowerShell scripts: 

  • Performing complex migrations 
  • Scheduling migrations 
  • Automating reports 
  • Scheduling reports, or just pulling information from your tenant 
  • Finding inactive and orphaned teams, as well as externally shared links and guest access, then automating messages to the relevant end users to resolve those issues 

The bottom line is automating tasks and having full visibility into your environment helps IT teams to stop the firefighting and finally focus on value-addition projects for their organization. 

Want to learn more about ShareGate? Start your 30-day free trial now! 

What did you think of this article?

Recommended by our team

Getting started is easy

Try ShareGate free for 15 days. No credit card required.

Hosts 1

LIVE VIRTUAL EVENT RISE UP: Ignite your M365 tenant migration playbook