A beginners guide to powershell – Part 3 – VariablessteemCreated with Sketch.

in #technology6 years ago (edited)

Welcome to the third part of my beginners guide to powershell series. Today I will introduce you to the concept of variables in powershell.

You can find the previous parts here:
Part 1 – Introduction
Part 2 – Getting started with powershell ISE

Variables

What is a variable?

In short a variable is a temporary storage that can hold values during the Script´s execution. In powershell the indicator for a variable is the $ sign.

Working with variables

To declare a variable in powershell you have to assign a value to it. Here is a example you can try in ISE (either enter this directly into the console or write it in the editor pane and execute it):

$myVariable = 1
$mySecondVariable = „two“

You can now access the content of your variables by referencing it like this:

$myVariable

If you are already a bit familiar with programming you might have noticed that we did not define the type of our variables.
Gladly powershell does the type definition for you depending on what you assign to the variable. You can also force a certain type like this:

[typename]$variable

Here is a list of the basic types:

The two that might not be self explanatory are:

Array

An array is a collection of objects, they are organized by an index that starts at zero. Example:

IndexValue
0"String1"
1"String2"

You can create an array like this:

$array = @() # empty array
$array2 = „String1“,“String2“,“String3“
$array3 = 1,2,3,4

And access the value like this:

$array3[0] #(for the value at index 0)

Powershell even allows you to mix types inside an array, don´t do that though!

Arrays can also be multidimensional, which means the array is storing other arrays like this (2D):

IndexValue
0"String1","String2"
1"StringA","StringB"

You can create a 2 dimensional array like this:

 $2dArray = @(@(„String1“,“String2“),@("StringA","StringB")

In this case you would access the values like this:

$2dArray[0][0] (for the value at Index 0,0)

Hashtables

A hashtable is a collection of Name-Value pairs and is very efficient at that. Here is an example:

NameValue
ZipCode01234
CountryAustria
CityVienna

It can be created like this:

$hashtable = @{ZipCode=01234; Country=“Austria“; City=“Vienna“}

And the values can be accessed like this:

$hashtable.ZipCode

Hashtables and arrays can also be mixed, so you could have an array of hashtables or a hashtable with arrays as values.

Objects

In addition to the basic types above variables can also store complex objects that can have properties and methods. As an example here is a „Document“ object:

Document
  • Properties:
    • Name
    • Length
    • Author
  • Methods:
    • Save()
    • Print()

Next up is an example that shows the usage of an object in Powershell. I think the szenario of pinging a computer is also the perfect one to give a good impression of some of the advantages powershell has over cmd.

First in CMD:

The first thing to notice is that the output is localized (german in this case). The output you get from ping is text so you would have to parse it.
Since I suck at it i can not even provide an example of how that would look like (maybe someone can provide one in comments) but it is not practical to write scripts for localized output when you have an environment with multiple languages.

Here is how it looks in powershell:

On the first look there is not much of a difference but the ouput we get is actually not text like in cmd but an object.
Let´s save the output to a variable:

$ping = Test-Connection google.com

This will give us an array of „Replies“ which have properties like „ResponseTime“, „Destination“, etc.. So if we wanted to know how long it took for the computer to reply on our first request we could do this:

$ping[0].ResponseTime

We could also do

$ping.ResponseTime

And get the values from all 4 entries.

The properties have the same names throughout all operating system languages so the problem of localization becomes nonexistent.

Next Up

First off a bit off topic: I am currently on holidays and am writing this part to let you know the series is not dead but it will take a bit for the next part to appear.
In the next part I will go into logical control constructs (loops, if-else etc). Stay tuned and if you have feedback let me know in the comments.

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.028
BTC 57497.94
ETH 3099.12
USDT 1.00
SBD 2.32