Mastering SharePoint Administration: Top PowerShell Commands You Need

Learn essential PowerShell commands for SharePoint Administrators to automate site provisioning, manage permissions, and enforce compliance policies, boosting efficiency and security.

Mastering SharePoint Administration: Top PowerShell Commands You Need
Photo by Hiroyoshi Urushima / Unsplash

As a SharePoint Administrator, you likely work with PowerShell daily to manage your environment. But are there specific commands that can significantly enhance your productivity? This article will delve into the top PowerShell commands tailored for SharePoint admins, providing insights on their functionality, usage, and benefits.

🚀 Bulk Site Provisioning

Automate the creation of multiple SharePoint sites using templates, CSV input, or predefined settings. This saves time by eliminating repetitive tasks.

🔧 Example Command

$sites = Import-Csv "C:\path\to\sites.csv"
foreach ($site in $sites) {
  New-PnPSite -Type TeamSiteWithoutMicrosoft365Group -Title $site.Title -TimeZone UTCPLUS0100_BELGRADE_BRATISLAVA_BUDAPEST_LJUBLJANA_PRAGUE -Url $site.SiteUrl -Owner $site.Owner }

Benefits: Speeds up site creation. Ever had to create the exact same Site to be used as Document Cabinet per year for the next 20 years. In 4 different environments (Dev, Test, UAT and Production?)...

Caveats: Run a small test sample. You don't want to remove 80 Site Collections due to misconfiguration.

💼 Bulk Teams & Group-Connected Site Creation

Provision Microsoft Teams and associated SharePoint sites in bulk, ensuring consistency in settings. If you can create these as templates for different use-cases, different departments, you can streamline the process of user-initiated provisioning. Add a pinch of approval request process and voilá, a full provisioning framework. For (almost) free. Spice things up with PnP Provisioning Schema if you feel fancy.

🔧 Example Command

# Provision new Team using the command directly
New-PnPTeamsTeam -DisplayName "Team Name" -MailNickname 
"teamnickname" -Visibility Private

# Apply the tenant provisioning template to create team and other stuff
Apply-PnPTenantTemplate -Path .your-template.xml -Parameters @{"TeamTitle"="My Team Title";"TeamAlias"="myteamalias"}

Benefits: Streamlined provisioning processes.

Caveats: Requires appropriate permissions and a whole lot of parameters configuration.

📚 List Template Automation

Provision Lists/Libraries in bulk from templates! Since SharePoint allows you to create custom List templates, you can design the list with everything you need and then save it as a template that can be reused. Similarly, I had to provision a lot of lists and libraries, segregating the data by months or days.

🔧 Example Command

$TemplateName = "My Special List Template"
$Year = 2025
#Get available List Templates from Site's gallery
$Ctx = Get-PnPContext
$Web = Get-PnPWeb
$RootWeb = $Ctx.Site.RootWeb
$ListTemplates = $Ctx.Site.GetCustomListTemplates($RootWeb)
$Ctx.Load($RootWeb)
$Ctx.Load($ListTemplates)
Invoke-PnPQuery
#Create monthly Special libraries for the year
$ListTemplate = $ListTemplates | Where-Object { $_.Name -eq $TemplateName }
$LibraryName = "Special Library"
for ($Month = 1; $Month -le 12; $Month++) {
    $MonthString = "{0:D2}" -f $Month    
    $LibraryMonthName = "$LibraryName-$Year-$MonthString"
	
    $ListCreation = New-Object Microsoft.SharePoint.Client.ListCreationInformation
    $ListCreation.Title = $LibraryMonthName
    $ListCreation.ListTemplate = $ListTemplate
    $Web.Lists.Add($ListCreation)
    Invoke-PnPQuery

Benefits: Saves time on configuration and provisioning, especially in bulk.

Caveats: Can become complex with many list settings. Run small tests first, before committing to large provisioning jobs.

⚙️ Activating Site Collection Features

Enable or disable features like publishing, content types, or metadata navigation across sites. Need to enable the Document Set Site Collection feature in the Site you're currently working with?

🔧 Example Command

Enable-PnPFeature -Identity 3bae86a2-776d-499d-9db8-fa4cdc7884f8 -Scope Site

Benefits: Improved usability especially with bulk provisioning.

Caveats: -Force will not work if you updated the feature with Update-SPSolution. In this case you have to disable feature first with Disable-SPFeature and then enable it back with Enable-SPFeature

🔐 Managing Permissions & Access Control

Grant, modify, revoke permissions at scale or configure properties of an existing user.

🔧 Example Command

#Give user Contribute access to a specific List
Set-PnPListPermission -Identity 'Documents' -User 'user@contoso.com' -AddRole 'Contribute'
#Create custom Permission Level by cloning existing and modifying it
Add-PnPRoleDefinition -RoleName "Contribute with no delete" -Clone "Contribute" -Exclude DeleteListItems

Benefits: Better security management. Quite useful with Just-In-Time approach to permission assignment, unless you want to do it via REST API (which is also fine)

Caveats: Mismanagement could cause serious access issues.

🏛️ Hub Site Association Management

Associate multiple sites with a Hub Site for governance. Associating a site with a hub site makes it easier to discover related content, news, and site activity across all associated sites.

🔧 Example Command

#Register a Site as a Hub Site
Register-PnPHubSite -Site "https://tenant.sharepoint.com/sites/myhubsite"
#Adds a Site to an existing Hub
Add-PnPHubSiteAssociation -Site "https://tenant.sharepoint.com/sites/newsite" -HubSite "https://tenant.sharepoint.com/sites/myhubsite"

Benefits: Enhanced navigation and user experience.

Caveats: You may want to draw it first to see if it's going to make sense to your users. UX, anyone?

🔍 User Activity & Security Reporting

Generate reports on properties, usage, security and compliance.

🔧 Example Command

#Get the Tabs not used modified in last 30 days.
Get-PnPTeamsTab | Where-Object { $_.LastModified -gt (Get-Date).AddDays(-30) }
#Get the guest users of Sales M365 Group.
Get-PnPMicrosoft365GroupMember -Identity "Sales" | Where-Object UserType -eq Guest

Benefits: Reporting, reporting, reporting. An insight is always welcome, isn't it?

Caveats: You need to have an idea of the actual insights you want to produce!

🗑️ Scheduled Maintenance & Cleanup

Automate cleanup tasks such as deleting unused sites, removing inactive users, and archiving.

🔧 Example Command

Get-SPOSite | Where-Object { $_.LastContentModifiedDate -lt (Get-Date).AddDays(-365) } | Remove-SPOSite

Benefits: Reduces clutter.

Caveats: Risk of accidental deletion. You may want to ask for approval first.

📜 Provisioning & Managing SharePoint Online Policies

Enforce retention policies, versioning, and compliance rules.

🔧 Example Command

#Sets the Alpha retention label to specific list
Set-PnPRetentionLabel -List "Invoices" -Label "Alpha"
#Sets the Alpha retention label to specific list and syncs to all items
Set-PnPRetentionLabel -List "Invoices" -Label "Alpha" -SyncToItems $true
#Sets the Alpha retention label to specific list and syncs to specific items
Set-PnPRetentionLabel -List "Invoices" -ItemIds @(1,2,3) -Label "Alpha"

Benefits: Ensures better compliance.

Caveats: Requires understanding and management of retention policies.

Utilizing these PowerShell commands empowers SharePoint administrators to streamline processes, enhance governance, and maintain efficient site management across the organization. Automating tasks can significantly reduce time and error rates while keeping your SharePoint environment in shape!