Platinum Sponsor

PowerCLI: Lesson #4

In this lesson we are still focusing on the basic fundamentals of PowerShell. I know this section should be about PowerCLI but without understanding the fundamentals of PowerShell then nothing we discuss about PowerCLI will make sense. I will still try to throw some PowerCLI cmdlets into the mix though. So that being that I did promise to talk about PowerShell variables in more detail so here we go.

Using variables in PowerShell allows you to store objects or details in to an area of memory for later use.  Without the use of variables it would be more or less impossible to perform calculations with PowerShell or even do things like manipulate filtered data. This becomes important in terms of managing VMware using PowerCLI as the amount data stored in the form of objects (VMs for example) and properties retrieved from VMware is immense.

Let’s simplify: A Variable is a dedicated area of memory in which you store information in for temporary use. You can create a variable to hold the number “5” then later with some calculations you could change the number to “10”. This value is always referable to while the variable exists. Variables are not restricted to numbers. You can create a variable to hold a string of text known as a string variable, or even a date or even an object like a VM with all its properties.

Below is a list of some types of variables that can be created in PowerShell.

Shortcut Data Type
[datetime] Date or time
[string] String of characters
[char] Single character
[double] Double-precision floating number
[single] Single-precision floating number
[int] 32-bit integer
[wmi] Windows Management Instrumentation (WMI) instance or collection
[adsi] Active Directory Services object
[wmiclass] WMI class
[Boolean] True or False value

It also has to be noted that there are predefined variables which we’ll look at in a later lesson. Also all variables are listed in the PowerShell variable provider, again something for another lesson. We need to keep it simple until later.

In other development languages it’s often required that you predefine a variable with the type  of data that it will store, for example a number or a string. In PowerShell that’s not necessary. PowerShell cmdlets and scripts are short lived unlike an application that you might create using C# or VB. So it’s common to declare the type of variable you will use  with these languages to optimise the memory space required by the variable.

In PowerShell you create a variable by choosing name for the  variable and preceding it with a $ character. for example:


You can then assign a value to that variable using the assignment operator (we’ll talk more about operators in a later lesson) which is the = character. So for example if you wanted to assign the $myvariable with the number “5” then you would enter:


Officially you could use the New-Variable cmdlet to create a variable but no one does that from the command-line.

So the next thing to know is once we have a variable declared we are now playing with objects. If we create the $myvariable variable it is too an object and just like all objects it has properties and methods (as described in lesson #3). If we create a string variable $mystring and assign it some text we can manipulate this object with its properties and methods. Different types of object have different methods and properties so let’s take a look see.

We first create and assign the variable and pipe it though Get-Member (gm) to see its methods and properties.


As this is a variable of type “string” so we can do stuff to it as if it was a string.

So we could call its ToUpper method which would display the text in Uppercase.


Calling this method doesn’t actually change the value in memory and you’d notice that when I try again to call the string without the ToUpper method nothing has changed since we first assigned the string.

Ok so let’s jump back into PowerCLI for the last section and apply what we know about variables with what we learnt in previous lessons.

If you remember we can list out which of our VMs are in powered off state by filtering the Powerstate property of VM Object. Now what we can do is store that list of a VMs in a variable object like so:


Now we can treat this variable like a VM object. To prove my point lets list its members (methods & properties)


So now if we want to start this list of VMs and we have to do is just pipe the variable into the Start-VM cmdlet:


Ok let’s call that a day for variables.


One Response to PowerCLI: Lesson #4

  • You know I’ve been searching the net for an explanation of variables and their usage in PowerCLI all morning and this is the first post that makes sense to me.

Leave a Reply

Gold Sponsors
Silver Sponsors