Powershell

As I learn more about Powershell, I really appreciate the Power it holds.

One of those Powers is the Hashtable.  A variable of indescribable power.  I’ll try to bring you new users in with this.  It’s really cool. 🙂

Normally in Powershell you just define a variable like this.

 

$FIRSTNAME=’Johnny’

or

$NUMBEROFCPUS=6

 

Or possibly you’ve done an Array such as

 

$Weekdays=”Sunday”,”Monday”,”Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday”

 

A Hashtable is a lot like an Array, and yet completely NOTHING like an array.   Normally an Array I can do this.

 

$Weekdays[3]

 

and get the result of

 

Wednesday

 

But a Hashtable can have one extra piece into it.  I can Group various TYPES of information into one list.  Alphabetic, Numeric, SecureString… If you can store it in Powershell, you can store it in a Hashtable.  It’s almost a MINI database when you’re done. So normally I might have a few arrays like this.

$Divisions=”Accounting”,”Manufacturing”,”Infrastructure”,”Development”

$Positions=”Manager”,”Assistant”,”Bookkeeper”

$Payscale=100000,75000,50000,25000,10000

 

But as a hashtable I can group all of this possibly dis-similar information together as one Group.   It’s done by using the @{} as part of the variable assignment and assigning a variable WITHIN that.  So I could rewrite all of this as.

 

$EmployeeDetails=@{Divisions=”Accounting”,”Manufacturing”,”Infrastructure”,”Development”}

$EmployeeDetails=$EmployeeDetails+@{Positions=”Manager”,”Assistant”,”Bookkeeper”}

$EmployeeDetails=$EmployeeDetails+@{Payscale=100000,75000,50000,25000,10000}

 

And done like this I can now access them as

$EmployeeDetails.Divisions[3]

or

$EmployeeDetails.Payscale

 

The situations where you may or may not want to utilize a Hashtable are up to you.  But it’s just another way information is accessed.   Even if you don’t heavily use this, understanding HOW the information is created and accessed can help YOU further in learning how to navigate data through Powershell.

 

“Tabled for your consideration…”

Sean
The Energized Tech

Advertisements