If you were one of the two people who read my last post on WSS and PowerShell I updated it a bit and it’s worth checking out the changes.

I thought this time I would just point out some of the very simple statements in PowerShell that differ from your usual C# so that you can quickly make a transition from one to another.

One of the first things any programmer might need to do in a script is conditions so here is a PowerShell condition and how it differs to c#

if ($i -eq 10)
    write "It's ten you know"

The comparison operator for equals is not "=" or "==" but a scripting style operator of -eq. So this is the first thing you’re going to have to remember.  Other operators are

-gt greater than,  -lt less than,  -ge greater than or equal to,  -ne not equal

There are more I found a simple table of them here.

When dealing with boolean responses its important to know that they are represented by $true and $false so a condition to test this would look like

if ($kidding -eq $false)


    write "How strange"


Now being good little programmers you’re bound to want to structure your scripts, so functions!  PowerShell has functions, but remember this is not a compiled language but a scripting language, this means you have to declare your functions before you can call them, this is not VBScript where you could have your main at the top and functions below, your execution code starts at the bottom after you declared your functions. 

So declaring a function can be as simple as (there are other ways but I’m sticking with this one for now)

function GoGetMyList($name)
    return $list

The function has a parameter of $name and returns $list.  Now the observant of you will have noticed that I created my $list on the fly giving it a scope of function level, but I access $lists whose scope is outside of the function, ok bad coding perhaps, but it’s to prove a point.

Variable scopes.  There are I believe three scopes  Global, Script, Stack (function) level.

Global scope is used in the PowerShell environment mostly you can set something of Global level in a script and it will be available on completion of the script in the scripting "Shell/Command Prompt" environment, if you are chaining scripts together this could be useful.

Script scope.  This is like a global or module level variable to your script code, once declared it’s accessible inside and outside all your functions

Stack/Function scope.   Declare a variable inside a function it’s scope and life-span are inside that function.

The weird thing is how you declare and reference these variables.  So here is how to define 3 scopes within a function

function GetBaseObjects($SiteUrl)

    $script:site = New-Object Microsoft.SharePoint.SPSite $SiteUrl
    $script:web = $site.RootWeb
    return $item

Now this is a thoroughly useless function but it demonstrates the differing levels.

$script:site = New-Object Microsoft.SharePoint.SPSite $SiteUrl

The $scipt:site  declares the variable at a script level accessible from another function or the script "main body", there is no need to pre-declare these variables because at EVERY assignment you make it sets the scope, this is a valid set of statements

function dave()
    write $tellme
    write $tellme
write $tellme

This will display "Script", "function" and "Script".  This is because PowerShell with search each scope for a variable and if not there it looks up the inherited variable scope tree, for want of better words.  The first time referenced we have a script level but the second time we have a function level and the third time the function itself is out of scope along with its variables so we access the script level one again.

So remember the order of your functions when setting scope level variables and ALWAYS when setting a variable to define the scope.

That’s all for this post, I’ll try and do another one encompassing error handling and arguments in a complete script that changes SharePoint settings.

Technorati Tags: ,,,