A PowerShell script is really nothing more than a simple text file.
The file contains a series of PowerShell commands, with each command appearing on a separate line.
For the text file to be treated as a PowerShell script, its filename needs to use the .PS1 extension.
If you want to use the variable, simply call it by name and prefix with '$' symbol.
$firstVarible = "This is a test" (Assign text to varible)
$firstVarible (Print varible value output should be "This is a test")
$firstVarible = $firstVarible + " Added text"
$firstVarible (Print varible value output should be "This is a test Added text")
Built in variables in PowerShell.
[string] Fixed-length string of Unicode characters
[char] A Unicode 16-bit character
[byte] An 8-bit unsigned character
[bool] Boolean True/False value
[int] 32-bit signed integer
[single] Single-precision 32-bit floating point number
[double] Double-precision 64-bit floating point number
[long] 64-bit signed integer
[DateTime] Date and Time
[decimal] A 128-bit decimal value
[array] An array of values
[hashtable] Hashtable object
[xml] Xml object
Launch PowerShell and let’s get started…
Think of a variable as an imaginary box where we store information. Two basic uses of variables are:
In PowerShell, variables can contain text strings, integers, and even objects (complete with properties and methods). Special variables exist, which are pre-defined within PowerShell. We have worked with one of these variables ($_) in a prior tutorial.
Special Variable Examples
To view the complete list of Special Variables in PowerShell, type the following:
In VBScript, we have to declare variables before using them. For example:
In PowerShell, ALL variable names must start with the “$” character. Once data is assigned to a PowerShell variable, it’s automatically declared. The “=” operator is used to assign data to a variable. Here is a simple example of creating a variable in PowerShell:
There is a “Set-Variable” cmdlet that can also be used:
You can use virtually any variable name you choose, names are not case sensitive. Note: there are illegal characters such as; ! @ # % & , . and spaces. PowerShell will throw an error if you use an illegal character.
I stated virtually any name as PowerShell does have a set of keywords that are reserved and cannot be used as variable names:
When naming variables, I continue to use the caMel case naming convention I became accustomed to in VBScript. The first part of the name (str) reminds me that the variable I’m working with contains “text string” data. If I were working with numbers, it might look like something this “$intMegaBytes.” Again, you can use any naming convention you wish.
Let’s verify that the $strComputer variable is holding the data we assigned to it.
-or just type-
The output verifies the data in the $strComputer variable is the “Computer1″ text string. Go ahead and verify the data value for the “strUser” variable.
Did you get John Doe for the output? Yes… great! No?… I knew you were going to skip the long version of using the “Set-Variable” cmdlet.
Note: If you use the “Set-Variable” cmdlet and specify the -Name parameter, you do not use the “$” character when defining variables. Most of the time I use ($strComputer = “computerName”) to create variables. Just be conscious of this rule when using the “Set-Variable” cmdlet.
Working with Strings
A [sting] is a “text string” data type. There are many data types that we will be working with in PowerShell:
Yep, we will be discussing them all at some point…
Concatenation is the process of joining together two strings. Here is an example:
We used += to join together variables $strA and $strB and store the new “text string” in variable $strC. The output from $strC is Hello World!
Most of the time you will see two strings joined together by using just the “+” operator. Like so; $strC = $strA + $StrB. Both result in the same output.
Aside from joining strings we can also replace words using the -replace parameter.
Go west young man!
What the heck is Interpolation?
Quotes are used when working with string data, either double-quotes(” “) or single-quotes(‘ ‘). What’s the difference? Glad you asked… It’s called Interpolation. As stated at the beginning of this PowerShell training session, variables are stored for later use. Interpolation occurs when a string variable is enclosed in double-quotes and does not occur when single-quoted. Let me give you an example of what this means:
By double-quoting, the output of the “Write-Host” cmdlet is – Tom drives a fast car. This is interpolation, PowerShell substitutes the value of variable $strA. Let’s see what happens when using single-quotes:
Output – Tom drives a $strA car.
PowerShell does not substitute the variable.
Working with Numbers
PowerShell works with a number of data types, for example integers(9) and decimals (9.9). The PowerShell engine can automatically recognize the data type assigned to a variable. For example, it knows that $x = 1 is an integer data type. It also knows that $x = “Hello World!” is a string data type.
Assigning integer and decimal values are simple:
There are those rare occasions when PowerShell may have not assigned the proper data type to a variable, causing havoc with your script. Though it is not required, it is considered good form to assign the data type of a variable. Refer to the data type table presented earlier in the tutorial. Here are some examples:
PowerShell follows the rules of math. Equations are read from left-to-right, multiplication and division are preformed then addition and subtraction.
What answer did you get? Hopefully 33.
Parenthesis can be used to alter the order of the mathematical rule. O.k. enough math class.
Note: PowerShell has two additional operators that you will use frequently. Make a mental note of these operators as you will be using them often in your script writing.
We will work with more data types when the advanced PowerShell tutorials are launched.
Here are nine exercises to complete. See if you can get through them all without looking at the answers. Answers provided below the exercise: