Variables

I’m going to act like you’re brand new to scripting. So think of a variable as a bucket you can store a type of data in that is referenced as a word. Also note that variables in PowerShell start with a $ symbol. For example, we can create a variable called $FirstName that contains the word “Matt” inside. So when you go to get the information from the variable $FirstName it will return Matt.

Here is an example in code of how variables are used.
NOTE: Anything starting with a # is a comment

# This creates a variable $FirstName and we store the word Matt in it.
C:\> $FirstName = "Matt"

Now if we type in the variable in PowerShell and press enter it will display the value to the screen.

C:\> $FirstName
Matt

There will be more content around PowerShell Objects later, but I want you to know that this variable we created is an object of the type string. These are some of the most common types you will come by when scripting

  • String – Same thing as text. Anything that has quotes around it.
    • Example: $variable = “This is a string”
  • Int – Short for integer this is any number without a decimal point
    • Example: $variable = 100
  • Float – This is a number with a decimal point
    • Example: $varable = 100.00
  • Array – List of items.
    • Example: $variable = “Apple”,”Orange”,”Lime”

Integers

Creating an integer is simple. Just like creating a variable instead of setting it to a string (text) set it to a integer (number).

C:\> $number1 = 150

Now that we have are integer (number) in a variable lets test some things out. We can perform any math operation on this number that we like.

# Lets minus 50 from the number
C:\> $number1 - 50
100

Notice that when we minus 50 from are $number which is 150 we get the result printed to the screen of 100. Now if you if you check the value of our $number1 by typing the variable name an pressing enter you get the value stored in the variable which is 150. Why is it not 100? Because we didn’t tell it to store our new number in the variable only to display it.
Lets update our number this time!

# The results of the math on the right of the equal sign will be stored in $number1
C:\> $number1 = $number1 - 50

Notice you don’t get any output printed to the screen. That’s because it went into the variable $number1 updating it to our new number. Here are some items you can use with integers

SymbolDescription
*Asterisk symbol is used to multiplication
/Forward slash is used for division
+Used for additions. This can also be used with a string to concatenate them together.
Dash is used for subtraction

Arrays

An array is a list of items. You can add and remove items from this list. One thing you may run into is an array with a fixed size.

# Create a list
C:\> $MyList = "Apple","Orange","Lime"

Now if we try to use the Add method we will get the following error

C:\> $MyList.Add("Banana")
Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."
At line:1 char:1
+ $mylist.Add("Banana")
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NotSupportedException

Our Array is set to a fixed size based on the above error “Collection was of a fixed size.” You can add to this array by replacing the the list with a new one. You can also create an array that doesn’t have a fixed size. Both examples are below.

# Replace the old array with a new and add an item. This will add "Banana" to the list
C:\> $MyList = $Mylist + "Banana"
C:\> $MyList
Apple
Orange
Lime
Banana

# You could also do the following to add to a fixed array
$MyList += "Banana"

To create a not fixed array where you can use the method .add() do the following

# Create an array that isn't a fixed size so you can use the Add method
[System.Collections.ArrayList]$MyList = "Apple","Orange","Lime"

# Now lets use the add method and check our list
$MyList.Add("Banana")
C:\> $MyList
Apple
Orange
Lime
Banana

Leave a Reply

Your email address will not be published. Required fields are marked *

Close Bitnami banner
Bitnami