Hi, Damian Garbus here and this is my next post. A few weeks ago I wrote two article about the variable. The first one “My way to understand Powershell variable for everyone.” is some kind of post to introduce the subject of the variables. The second one “Powershell variable types. Why sometimes 1 + 1 is 11” describe basics types of variables. Now, it’s time to show the next variable types: Array and Hashtable. Let’s Start.

Array and Hashtable

In this post, I want to explain the next variable types: “Array” and “Hashtable”. These are special types of variable that can contain many items. In the earlier posts, I have used the box as an example to explain what is variable and what are their basics types. In this post, also I will use the box o paper documents to explain how to understand these next types.

The box o paper documents

Array

As I wrote it above, the array is a special variable type that can contain one or more items. Now you can imagine that your variable is the empty box. You want to put into this box one or more paper documents. This box is like an array type of variable. Please remember, you can put into the box paper documents but also you can sort them. If documents have the title you can take one or more documents from the box and put into another box (another variable).

$box = @()
$box = "Document number 2","Document number 4","Document number 3","Document number 1"
$box

$box | Sort-Object
$box | Sort-Object -Descending

$box = $box + "Document number 6"
$box
$box += "Document number 5"
$box

$box[1]
$box | Where-Object {$_ -eq "Document number 3"}
Array and Hashtable

Hashtable

Another type of variable is the “Hashtable”. It’s similar to “Array” type but is one very important difference. Can you imagine another empty box? You want to put into this box one or more paper documents. But this box is amazing. This box requires from you, that before you put into this box these paper documents, you have to mark one by one using a sign, such as sticky notes. In the”Hashtable” type variable, you have to use the “key” value. Now you can use these sticky notes (“key”) to search documents (items) in the box (variable).

$AnotherBox = @{}
$AnotherBox.Add("Green Sticky Notes","Document 1")
$AnotherBox.Add("Yellow Sticky Notes","Document 2")
$AnotherBox.Add("Red Sticky Notes","Document 3")
$AnotherBox.Add("Blue Sticky Notes","Document 4")



$AnotherBox['Blue Sticky Notes']
Array and Hashtable

Conclusion

As you can see, next variable types are also easy to understand. Now if you want to read another (more technical) explanation of array and hashtable types I recommend post Everything you wanted to know about hashtables. At the end I want to leave you with one sentence – reading about it is not enough. Please run now Powershell console and try to understand with these examples. If you have some simple example how to understand array and hashtable. Please write in the comment for others readers.

Get Access to Poshland Resources for FREE!!!

Signup now and receive an email once I publish new content.

I agree to have my personal information transfered to MailChimp ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.


Damian Garbus

Hi, My name is Damian. I'm Powershell geek. Also, I love to improve my productivity and yourself development. I connected Powershell and my productivity tricks and built this blog. You can find here how to learn Powershell and use this to improve your productivity. Enjoy.

Leave a Reply

Your email address will not be published.