Quantcast
Viewing all articles
Browse latest Browse all 9

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.

Image may be NSFW.
Clik here to view.
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.

Image may be NSFW.
Clik here to view.
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

Image may be NSFW.
Clik here to view.
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.

Image may be NSFW.
Clik here to view.
sharepoint-powershell-getting-started-cameron-dwyer-run-script

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

Image may be NSFW.
Clik here to view.
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 Image may be NSFW.
Clik here to view.
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).

 


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 9

Trending Articles