Get Started With SharePoint 2010 Management Shell

  • It is PowerShell

With the installation of the SharePoint 2010, you can get a tool called Management Shell. It is a brand new tool for SharePoint, and it is PowerShell in fact. Be frankly, I don’t know anything about PowerShell before until me read a blog about how the using of PowerShell can easy the administration of SharePoint 2010. So why don’t give it a shot, I think. Just read a few of posts and the PowerShell manuals, I have done a little of work with PowerShell, and I am really satisfied with what it can do. ?In this post, I want to show you a piece of code I just wrote to share my experience as a very beginner.

  • Why do I need this function

Recently, I have to do a lot of work with List, Item and Fields. Without PowerShell, I have to open a browser to access my site to get the work done. When I have to do the same work on browser again and again, it really get very inefficiency. Can you imagine how boring it is when you create a list repeatedly just because you need it to do some test or debug, especially when you do it manually on browser? I wish I could know Mr PowerShell much earlier. But I am not going to show you that how PowerShell can create a List in SharePoint. Instead of that, I want to show you how to get an SPListItem in a List, and how can we get a Field of it.

  • How can we get it work

function Get-SPItem([string]$site=$('http://localhost'), [string]$web=$('Home'), [string]$list=$(throw 'Parameter -list is missing!'),[int]$item=$(1),[string]$field){
$siteObj = new-object Microsoft.SharePoint.SPSite($site)
$webObj = $siteObj.openweb($web)
$listObj = $webObj.Lists[$list]
$itemObj = $listObj.getItemByID($item)
if($field.length -gt 0)	 {
}	 else	 {

Since the Logic of this function is very simple, I won’t explain so much about it. Instead of that, I want to share with you my feeling about PowerShell based on this code snippet.

  1. The statement of the function parameters.Because the most of our development are done on just one site, so assign the default value is very necessary. But if we implement this idea in C#, we have to add a default value statement for $site in the body of function(Method, if you insist), and even worse is that we also need a null-value check to make sure that we won’t override the value that the end user really assigned. But here, you can take a look at the statement of the first parameter:[string]$site=$(‘http://localhost’).?”http://localhost” will be the default value when user skipped this parameter. I am sure this is more convenient. There is another example worth to take a look is the $list para. it is also easy to understand the syntax about it. Since the list is a required part of the function, and the working list will change all the times. So we have to make sure the list parameter is assigned before using it. Again, if we do it in C#, we still need a null-value check, and tell the end user if they missed it. But look at it here, isn’t is just more lovely?
  2. Create an object. ?PowerShell is?object oriented script. And more than that?PowerShell is based on .NET so you can easily look at the properties, call the methods, even use events etc. Generally speaking if the task can be done in .NET, then it can be done in PowerShell. The typical task might be: go through all the files in given directory, open them and replace string XYZ with ABC. So you can understand why the first statement in the function body can work. And also, you can know why I can use the Property Lists of $webObj, the method GetItemByID of $listObj, and even the get_Item( this is generate for Index of SPListItem by the .NET compiler) method of $itemObj. By that, we can implement any functionality with a Document of .NET. Actually, that is how I get this function and all my other PowerShell functins done.
  • How to use it

Like all the programming language, we need to tell the system the function we defined before we can use it. In PowerShell, there is a file called a profile that we can put all our custom functions there. After that, the PowerShell runtime will load these functions automatically before we can type in commands.

To test your if you have a profile defined already, use this command:

test-path $profile

if the result is false, that means you need create one for your own. Here is the command:

new-item -type file -path <profile-name> -force

After that, you can open it in a notepad by

notepad $profile

If you want to know where this profile placed, ?just type:


So the only thing left is to call the function, here is an example:

$item = get-spitem -list "Shared Documents" -item 4

$field = $item.fields["title"]

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
The following two tabs change content below.

Leave a Reply