How to Remotely Execute SharePoint PowerShell Cmdlets?

PowerShell allows us to run cmdlets remotely from client machines. We can run SharePoint cmdlets on the SharePoint Server, remotely connecting from client machines, by following these two steps:

Step 1: Enable PowerShell Remoting

To execute SharePoint PowerShell remotely, you must first enable PowerShell Remoting on the server! By default, PowerShell Remoting is disabled. Log-On to the server you want to access remotely, enable remote PowerShell for running SharePoint 2010 cmdlets

Enable-PSRemoting
execute sharepoint 2010 powershell remotely

This command enables remote PowerShell for SharePoint 2010 by enabling WinRM service and configuring Windows firewall to allow incoming sessions. It’s a one-time activity to configure remote PowerShell in SharePoint 2010.

Step 2: Execute the PowerShell Script on the Remote Machine

Now, We can either directly invoke PowerShell Cmdlets or establish a session and then execute PowerShell cmdlets. E.g., To get the total no. of site collections, I’m using the below code:

Option 1: Invoke PowerShell remotely to run SharePoint 2010 cmdlets

To Invoke SharePoint 2010 PowerShell on the remote server:

 Invoke-Command -ComputerName SPSWFE01 -ScriptBlock { 
 Add-PSSnapin Microsoft.SharePoint.PowerShell; 
 $webApp = Get-SPWebApplication "https://sharepoint.crescent.com"; 
 write-host "Total No. of sites in the Web Application: $($webApp.Sites.Count)" 
 } -credential (Get-Credential)

Option 2: Establish a Remote Session and then execute PowerShell cmdlets

We’ve to connect to a remote SharePoint server using PowerShell. In the Client machine, enter:

Enter-PSSession "SharePoint Server Name" -Credential (Get-Credential)

This cmdlet prompts for credentials and opens a PowerShell remote session from the remote SharePoint farm. Once done, we can run PowerShell commands. Here is an example to establish and execute PowerShell commands

Add-PSSnapin Microsoft.SharePoint.PowerShell
$webApp = Get-SPWebApplication "https://sharepoint.crescent.com"
write-host "Total No. of sites in the Web Application: $($webApp.Sites.Count)"

Don’t forget to exit the PowerShell session once you are done with PowerShell:

Exit-PSSession

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *