How to Remotely Execute SharePoint PowerShell Cmdlets?

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

Step 1:

To execute SharePoint PowerShell remotely, you have to enable PowerShell Remoting on the server first! 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 configures Windows firewall to allow incoming sessions. It’s a one-time activity to configure remote PowerShell in SharePoint 2010.

Step 2:

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 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 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 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 - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

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