PowerShell Gems That Every Microsoft 365 Administrator Should Know About

Published: Mar 03, 2021

SHARE ARTICLE

As an IT Professional, can you survive without PowerShell? I can imagine there are several debates around this topic on Reddit right now. The short answer is yes, you probably can (barely). However, keeping PowerShell as your go-to tool on your IT Pro toolbelt will bring you varying productivity and efficiency boosts. Quite simply, the more you use it, the more you will accomplish in a shorter amount of time. Your supervisor/manager will rarely, if ever, complain about you completing your Microsoft 365 Admin tasks earlier than promised or needed. Plus, your users and employees you support will always be grateful their support issue or service request is completed in a timely manner! I’ll explore the various Microsoft 365 online services and how PowerShell can help you get your job done.

Note – Wondering how to efficiently connect to all the core Microsoft 365 online environments with a single PowerShell script? Don’t worry, I’ve got an example script at the end of the article.

Let’s start with SharePoint Online (SPO). To create a new Site Collection, use the ‘NewSPOSite’ cmdlet:

New-SPOSite -Url https://tenant.sharepoint.com/sites/mynewsite -Owner [email protected] -StorageQuota 1000 -Title "Welcome to PowerShell"

This will create a new site with the above URL, the Primary Admin of the site collection being the ‘Owner’, setting the Quota for the site to about 1 GB, and giving it a Title.

If you want to add yourself or another admin on your team to a site as a Site Collection Admin, you can use the ‘Set-SPOUser’ cmdlet.

Set-SPOUser -Site https://tenant.sharepoint.com/sites/sitename -LoginName [email protected] -IsSiteCollectionAdmin $true

Let’s move on to Teams! Microsoft has been increasing the breadth of cmdlets for Teams since Day 1. Let’s start with creating a new Team; you’ll accomplish this with the ‘New-Team’ cmdlet:

New-Team -Name "PowerShell Reference" -Description "Collaboration space to include PowerShell cmdlets for the Team”

Whoever creates the Team will automatically be made an Owner (admin) of the Team. Before we add an additional administrator as an ‘Owner’ of the team, we first need to identify the Microsoft 365 Group’s ‘GroupId’ by using the ‘Get-UnifiedGroup’ cmdlet. To avoid a potential human error with copying and pasting strings, let’s store the result in a string, ‘$GroupId’:

$GroupId = Get-UnifiedGroup -identity [email protected] | fl ExternalDirectoryObjectId

Or

$GroupId = Get-UnifiedGroup -identity “Title/Name of M365 Group” | fl ExternalDirectoryObjectId

The GroupId of the Microsoft 365 Group is stored in that string. To add an administrator as an Owner of our Team, we use the ‘Add-TeamUser‘ cmdlet:

Add-TeamUser -GroupId $GroupId -User [email protected] -Role Owner

The next service is Azure Active Directory (AAD). A recent service request I handled was to create a new Microsoft Plan for a user based on an existing Plan. That’s easy enough using the Planner website (Copy Plan). However, the user also wanted the 55 Members of the existing Plan (Microsoft 365 Group) added to the new Plan (Group). Well, you can understand how tedious and time-consuming (Oh No!) that would be adding those users one by one with a website Admin Center interface or even PowerShell one command at a time. Let’s use the scripting capabilities built into PowerShell to save the day!

First, let’s store the membership of the existing Microsoft 365 Group in a string called ‘$members’ using the ‘Get-UnifiedGroupLinks’ cmdlet:

$members = Get-UnifiedGroupLinks -identity ExistingGroupEmailAddress -LinkType Members

In my scenario, those 55 members’ info is now stored in $members. Let’s go ahead and add them, ‘one-by-one’, using PowerShell scripting via the ‘Add-UnifiedGroupLinks’ cmdlet:

foreach ($item in $members) {Add-UnifiedGroupLinks -identity NewGroupEmailAddress -LinkType Members -Links $item.primarysmtpaddress}

You can verify this magic worked by displaying the members of each group:

Get-UnifiedGroupLinks -identity ExistingGroupEmailAddress -LinkType Members
Get-UnifiedGroupLinks -identity NewGroupEmailAddress -LinkType Members

They should be the same.

Note – Only Members, not Admins, of the Groups, will be copied. Instead of ‘-LinkType Members’ use ‘-LinkType Admins’.

Here’s the Bonus PowerShell script I promised. This will connect you to the following services:

  • Azure Active Directory (AAD)
  • SharePoint Online (SPO)
  • Skype for Business Online (SfBO)
  • Exchange Online (EXO)
  • Teams
$acctName="[email protected]"
#Azure Active Directory
Connect-AzureAD
#SharePoint Online
Connect-SPOService -Url https://yourcompanyprefix-admin.sharepoint.com
#Skype for Business Online
$sfboSession = New-CsOnlineSession
Import-PSSession $sfboSession
#Exchange Online
Connect-ExchangeOnline -UserPrincipalName $acctName -ShowProgress $true
#Teams
Import-Module MicrosoftTeams
Connect-MicrosoftTeams

The only prerequisite implied here is that you have all the pertinent PowerShell modules installed before running the script.

Please feel free to leave any comments about the content and if you’re interested to see more in-depth coverage of specific Microsoft online services or even specific PS cmdlets!

SHARE ARTICLE