Jun 25, 2008

PowerShell & SharePoint - Quick Start

SharePoint and PowerShell are a perfect match. Here's the PowerShell profile I'm using on my Sharepoint Dev. Boxes.

If you haven't a PowerShell profile yet, create it:

  • >new-item -type file $profile -force
  • >notepad $profile
  • Paste in the following snippet or download:
$12HivesDir = "${env:CommonProgramFiles}\Microsoft Shared\web server extensions\12\"
 
#load Microsoft.SharePoint.dll
[System.Reflection.Assembly]::LoadFrom("$12HivesDir\ISAPI\Microsoft.SharePoint.dll")
 
#returns the SPSite at the specified URL
function get-spsite ([String]$webUrl=$(throw 'Parameter -webUrl is missing!'))
{
   return New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList "$webUrl";
}
 
#returns the SPSite object from the specified URL
function get-spweb ([String]$webUrl=$(throw 'Parameter -webUrl is missing!'))
{
   $site =  New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList "$webUrl";
   return $site.OpenWeb();
}
 
#returns the SPList object from the specified URL and List name
function get-splist ([String]$webUrl=$(throw 'Parameter -webUrl is missing!'),
[String]$listName=$(throw 'Parameter -listName is missing!'))
{
   $site =  New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList "$webUrl";
   $web = $site.OpenWeb();
   return $web.Lists[$listName]
}
 
  • Save
  • >set-executionpolicy -executionPolicy remoteSigned
  • Reopen the PowerShell 

Now you have. Use it:

  • >$web = get-spweb http://localhost
  • >$web.Title = "Greetings from the Powershell"
  • >$web.Update()
  • Browse to http://localhost and see what's happen.

I've teamed up with Eric Kraus from Microsoft. You can find our joint project for PowerShell 2.0 called SPoshMod on CodePlex. Hope the first release will be availiable soon...

12 comments:

Anonymous said...

hi christian,

the script works flawless for me, cool!

anyways, you should consider to upload the script as .txt file, because if I c&p the script to my ps profile I run into troubles with linebreaks.

Christian said...

Thanks, you're right!
I've just updated the posting accordingly.
Now cou can download it.

Christian said...

Some ideas what you can do from Karine Bosch:
http://www.u2u.info/Blogs/Karine/Lists/Posts/Post.aspx?ID=9

Karine Bosch said...

Christian,
I just read your post. Thanks for suggesting the functions. Great post!
Karine Bosch

Anonymous said...

Christian,

the good part about this is, that it also works with WSS2.0.

because 12hive does not exist in old wss, you just need to adjust the path to the sharepoint dll.

[...]Microsoft Shared\web server extensions\60\ISAPI\Microsoft.SharePoint.dll

Ani said...

How Can I use this powershell script in dispform.aspx?

Actually I have infopath form library, then I customized search result, search result is working properly and displaying all the column, but in title it is linked with XML file name rather than dispform.aspx?id=???.

How can I get for each sharepoint inforpath form library line item?

Can you help me out?

Christian said...

Hi Ani,

you can't use the Script in an aspx-form. It only works in the PowerShell.

That the title column links the Form is by design.
What do you mean exactly with customized search result? You could affect the title column with a custom content type or customize the search result to show the column with the dispform.aspx?id=?.
Could you please explain the scenario more exactly.

Bye,
Christian

Jesper M. Christensen said...

Always nice to find great resource about Powershell to Sharepoint sites/lists.

Keep up the good work and thanks for the info.

Jurgis said...

Hi, post is very helphull.

How can I updaten a lits item.
after calling $item.Update()
I keep getting an error:

Exception calling "Update" with "0" argument(s): "You cannot call a method on a null-valued expression."

Thanks in andwance.

Christian said...

Hi Jurgis,
$item.Update() ist the right method. Seems you $item is null.
bye, Christian

Jurgis said...

Hi Christian,

Maby this will help.
I do the folowing steps:

$list = get-splist http://mysite Tasks
$item = $list.Items[5]
$item["Title"] = "Test1"
$item.Update()

And get the error.

Thanks in adwance.

Christian said...

Hi Jurgis,

code snippet looks good. Your exception seems caused through a null value in varibale $item, but then $item["Title"] = "Test1" should raise the error before update. Seems weird. Next week I'm in holiday so I can't help. Maybe my SPoshMod colleague Eric Kraus could help you http://blogs.msdn.com/ekraus/

Bye, Christian

Bye, Christian