Powershell basics part 4 – Variables,reserved words,special characters

Hello, thank you for reading my posts, if you read all my powershell basics parts, now you should already have some skills working with powershell. In this post we will continue the training, I will talk more about variables, strings, arrays and other useful powershell things.
Powershell has some reserved words.These are:
We can find out more about reserved words by typing Get-Help about_Reserved_Words. Here is the output of the command:

Get-Help about_Reserved_Words

These words were build with a special purpose so you cannot use them as an array, variable, function name etc. For example while was predefined to be used when you want a code to run as long as a condition is met. It is also called a loop; If was build to test a condition.

Now, let’s talk a little bit about so called escape characters. First let’s type the following in powershell Get-Help about_escape_character:

Get-Help about_escape_character
In this output we can see the list of special characters and also a short description of the purpose of special characters. To learn more about each of them we can type the following: Get_Help about_special_characters.The output of this command is:
Get_Help about_special_characters
I’m not going to describe each of the special characters because they are pretty good explained in this command.
Variables start with $ in powershell and they can be declared in several ways:

a single numeric variable : example $x=123

a collection of strings : example $x=” Hello there”  (A string is a collection of characters)\
a variable that contains specific data : example [Type of variable]$x=Value
[Type of variable] can be the following:
[int] – a 32 bit interger
[long] – a 64 bit integer
[bool] – a boolean value, it can be only true or false
[decimal] – a 128 bit decimal value
[array] – array of values
[xml] – xml object
[DateTime] – date and time
[single] / [double] – single-precision (32-bit), double-precision(64-bit)
[string] – a list o characters
[char] – 16 bit character
[byte] – 8 bit character
[hashtable] - hashtable object
some examples of these variable declaration would be : [int]$a=123;
for declaration of a boolean variable I made the following example:
Boolean variable Powershell
If you want a variable to have special characters in it’s name you have to put {}, for example ${hello!@}=”hello”

Another way to create a variable is :

New-Item variable:\x -value 12
New-Variable x -value 12
More about these two can be found by typing Get-Help New-Variable / Get-Help New-Item:
Get-Help New-Variable
Automatic variables are variables that store information about powershell. Type the following: Get-Help about_automatic_variables. Here is the output of the command:
Automatic variables Powershell
With variables you can do a lot of things starting from arithmetic operations, assignment comparison, logical etc. We talked about operators in a past post.
An Array is a list of values. In a array each position corresponding to an index value. An array is declared by a @ in a variable declaration. I will give you an example of array declaration:
I want to create an array that includes only numbers as follows: 1 2 3 4 5. The declaration of the array would look like this:
Powershell array
The same array can also be declared like this:
Powershell array declaration
And if you want to print just a value from the array you would type the command like this:
write-host $array3] – 3 represents the index value of the array in our example the index corresponds with the value as follows:
index value    : 0 1 2 3 4
number value : 1 2 3 4 5
So if we want to change the first value we type the following :
Powershell array declaration
More about arrays you can find out by typing Get-Help about_arrays :
Get-help about_arrays
From here we can see that you can manipulate arrays in many ways:
$array.SetValue(22,3) – this example changes the third element of the array with 22
$ary = $array[0,1,2,3 + 5..($a.length – 1)] – this example creates an array called ary that has the same elements of array exact index value of 4.
$t= 3,6,2
$s= 4,5,1
And the value of $z is:
Powershell sumation script
Furthermore let’s type the following: $w=$z.length. This function gives variable w the number of items that z has. If we type now Write-host w$, powershell will return value 6. $w=$z.count does the same thing.
To see the available methods for a variable type the following:
Powershell sumation script
OK, that’s it for this post. In the next post we will discuss about other cool powershell features.
I hope you’ve enjoyed this, thanks a lot for reading,
Have a nice day.

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