I feel guilty for writing this post since when PowerShell v1 went to beta I was invited but never had a look at it until now. Anyway, do you find yourself in the need to lookup site information? What I often do is break in my code and use the “Watch” window in Visual Studio to take a look at site settings not accessible through the UI. I am glad there is an easier way now and I am so excited about it like a 10 year old. You can create managed objects inside PowerShell and access/modify their properties, call methods etc. So what I will do next is load Microsoft.SharePoint.dll into a PowerShell session and open my local site. Then I will change the title and update!

Step 1: Load Assembly

[Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

image

Step 2: Create a Site and a Web Object

$Site = New-Object Microsoft.SharePoint.SPSite(“Http://localserver”)

$Web = $Site.OpenWeb()

 

Step 3: Display Current Title

$Web.Title

image

 

Step 4: Set Title and Update

$Web.Title = “Hello, from PowerShell v2!!”

$Web.Update()

 

You are done! Repeat step 3 or simply open your site to confirm the title change. Another interesting feature is if you type the variable name only (ex. $Web), you will get a full list of all properties of that object. So no more funny watch window operations!

Do not forget to dispose your objects. A notable behaviour of PowerShell is the fact that each line you type in the window is executed in a different thread!! This is for v1 – I cant confirm this for v2. What that means is you might not be able to dispose your site unless you do it in one line, each command separated with a semicolon.The behaviour is different in script files.

I hope you will enjoy this as much as I am.

Advertisements