PowerShell variable names

Ok so this stuck with me from my VBS scripting days and why prefixing variable names can help with debugging your script. It also helps with documenting your script as you go along.

Sure it’s easy to name a variable x,y or z. but your guess is as good as mine as to what kind of data it represents. That’s where prefixes can go a long way…

When scripting in VBS you’d normally add a Prefix to let you know what the object is all about. Here’s a short list of what I use in PowerShell:

 

Type Prefix PowerShell
Array arr $arrMyArray
Hash Table hsh $hshSItes
Collection col $colMyCollrction
PSCustomObject obj $objCurrent
Progress prg $prgProcess
Count cnt $cntUsers
Index idx $idxUsers
Integer int $intQuantity
Boolean bln $blnFound

Here’s an example:

Let’s say your using a hash table. Variable $hshUserProperties let’s me know I’m dealing with a hash table. So I  know what to expect when dealing with $hshUserProperties.

$hshUserProperties = @{
    FirstName = "Irwin"
    SurName = "Strachan"
    Initials = "I.C.A."
}

Now let’s say I was dealing with a [PSCustomObject] (Powershell v3) I’d use the Prefix “obj” to let me know this is a custom PowerShell object.

$objUserProperties = [PSCustomObject]@{
    FirstName = "Irwin"
    SurName = "Strachan"
    Initials = "I.C.A."
}

It might seem trivial now but trust me, when debugging your scripts it helps!

When you want to collect an array of objects just add

$colUsersInformation += $objUserProperties

and your good to go!

I’ll admit that the whole custom object was new to me at the beginning, so it took some getting use to.

Which isn’t to say it’s necessary to create a custom object ahead of time.You have the choice of either adding the custom object or making the hash table an object in the process:

$colUsersInformation += New-Object PSObject -Property $hshUserProperties

this will also get the job done. $hshUserProperties still helps out by letting me know that it’s a hash table and I need to do something with it “before” adding it to a collection.

Ok and now about the name

When defining your variable name, my preference goes to CamelCase. Sure you could follow it to the letter. My prefix always is in smallcase so my variable name starts each word with a Capital letter. $hshUserProperties is a hash table containing User Properties value.

It helps to give your variable a name that will help you decipher what it’s all about. And with the help of PowerShell_ISE tab completion, having longer variable names shouldn’t be an issue…

Well that my tip for defining sound PowerShell variable names…

Hope it’s worth something to you…

Ttyl,

Urv

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s