Getting Started: The Basics of Using PowerShell with SharePoint

I’m a SharePoint guy, and certainly no PowerShell expert. I know enough PowerShell to get things done in SharePoint from time to time. I can often go months without using PowerShell and talking to colleagues I don’t think I’m alone in the SharePoint community.

So if you are just starting out with using PowerShell to do something with SharePoint, or it’s been a while and you need a refresher on the basics then you’ve come to the right place.

Before we get started I’m writing this article based on SharePoint 2013 running on a Windows 2012 server.

The ground rules

1) Run your PowerShell scripts from the SharePoint server itself

It is possible to run PowerShell against a remote SharePoint server rather than on the server itself, but it requires some setup on the SharePoint server side and let’s face it, it’s not basic. If you need to do it you need to be searching for “Remote PowerShell in SharePoint”.

2) Use Windows PowerShell ISE (not SharePoint Management Shell)

PowerShell needs to be executed/run at a PowerShell Console or PowerShell Window – this window looks like a standard Command Line window and I don’t find it too inviting.

Instead of the SharePoint 2013 Management Shell, I use the Windows PowerShell ISE.

sharepoint-powershell-getting-started-cameron-dwyer-windows-powershell-ise

Why? It’s like comparing Visual Studio with Notepad. The ISE is an environment for developing PowerShell scripts that gives you nice syntax highlighting, debug with breakpoints, intellisense and more. It’s more like a development environment than a command line. I think you’d agree it looks slightly more advanced.

sharepoint-powershell-getting-started-cameron-dwyer-powershell-ise

3) Ensure the SharePoint PowerShell snapin is loaded

When using the “SharePoint 2013 Management Shell” (the ugly black one) it automatically loads a “snapin” which is basically a PowerShell extension that gives you a series of commands for working with SharePoint objects. When you use the Windows PowerShell ISE it has no idea of SharePoint, so you need to load the SharePoint snapin manually. The simplest way to do this is just to add the following code snippet to the start of all your scripts.

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
      Add-PSSnapin Microsoft.SharePoint.PowerShell
}

 

4) Save a script to file before you try to run it

Windows PowerShell ISE will have trouble running a script if you haven’t saved it to disk yet. PowerShell scripts are saved as files with a .ps1 extension.

 

5) Talk to SharePoint via the SharePoint PowerShell Cmdlets

Here’s a reference of all the SharePoint 2013 PowerShell Cmdlets you can use to work with SharePoint.

 

Your first script

Pre-flight checklist:

  • Start Windows PowerShell ISE
  • Add code snippet for loading the SharePoint snapin
  • Save the script as a .ps1 file

sharepoint-powershell-getting-started-cameron-dwyer-first-script

You can now start cutting and pasting examples from the internet and modifying them to work with your environment. The following 2 lines get a handle on the SharePoint website at the URL http://vs-server12 and then output the ID of the website.

To run the script and see if it work, click the Run button.

sharepoint-powershell-getting-started-cameron-dwyer-run-script

Any output from running your script is shown in the output window below the script editor.

sharepoint-powershell-getting-started-cameron-dwyer-output

You’re on your way, just remember you can only access the SharePoint server on which you are running your scripts. Accessing remote SharePoint servers is possible but you need to do special magic stuff to make that happen Smile

 

If you encounter an error along the lines of:

Get-PSSnapin : No Windows PowerShell snap-ins matching the pattern “Microsoft.SharePoint.PowerShell” were found.

You’ve ignored ground rule 4 and the pre-flight checklist by forgetting to save your script before running it (yes, I still do this too).

 

9 thoughts on “Getting Started: The Basics of Using PowerShell with SharePoint

Add yours

  1. I didn’t ignore rule 4 and saved my file. I get an error message when running the SnapIn code.

    PS C:\> C:\Users\xyzUser\Downloads\Powershell\SPPowerShellSnapIn.ps1
    Get-PSSnapin : No Windows PowerShell snap-ins matching the pattern
    ‘Microsoft.SharePoint.PowerShell’ were found. Check the pattern and then try
    the command again.
    At C:\Users\wongm03\Downloads\Powershell\SPPowerShellSnapIn.ps1:1 char:5
    + if((Get-PSSnapin “Microsoft.SharePoint.PowerShell”) -eq $null)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (Microsoft.SharePoint.PowerShel
    l:String) [Get-PSSnapin], PSArgumentException
    + FullyQualifiedErrorId : NoPSSnapInsFound,Microsoft.PowerShell.Commands.G
    etPSSnapinCommand

    Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version
    3.
    At C:\Users\wongm03\Downloads\Powershell\SPPowerShellSnapIn.ps1:3 char:7
    + Add-PSSnapin Microsoft.SharePoint.PowerShell
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (Microsoft.SharePoint.PowerShel
    l:String) [Add-PSSnapin], PSArgumentException
    + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.Ad
    dPSSnapinCommand

    Like

  2. Pingback: sharepointjungle
  3. Hi, How can I manage SharePoint related Powershell script from remote desktop. is it possible. above solutions works only on server.

    Like

    1. I haven’t tried doing remote Powershell. There is a fair bit of information on the internet for doing it but I think those are mostly just the SharePoint Admin Powershell scripts. As a novel alternative have a look at this article, it shows how to use the SharePoint Browser (free tool for remotely accessing SharePoint) and it has some remote Powershell capability build in.

      Like

  4. i am getting below error while running the script to add snap-in:

    File C:\Users\pankhuri.prabhakar\Desktop\loadSharepoint.ps1 cannot be loaded because running scripts is disabled on
    this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
    + CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnauthorizedAccess

    Please help me on this.

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑