Dive Into PowerShell: Part One – Using Help

Using the built in help is a great way to figure out how to use PowerShell. This post will cover how to use the built in help function and learn more about arrays and hash tables in the process.

Type in get-help about_arrays to see what included in the documentation about arrays.

SHORT DESCRIPTION
Describes a compact data structure for storing data elements.

LONG DESCRIPTION
An array is a data structure for storing a collection of data elements
of the same type. Windows PowerShell supports data elements, such as
string, int (32-bit integer), long (64-bit integer), bool (Boolean), byte,
and other Microsoft .NET Framework object types.

Ways to Declare an Array

Static Example:
$arr = 1,2,3,4,5

Dot Operator Example:
$arr = 1..5

The examples above will create an array of five elements and with values from 1 to 5.  You can also declare a specific data type to be used by an array, here is an array for only strings would look like:
[string[]]$arr = “Hello”,”World”,”!”

How To Use Getters and Setters
$arr.gettype()
$arr.SetValue(value,position)
$arr.GetValue(position)

There still more content covered in the arrays help file, like using for and foreach loops.  Now let change gears and here is what PowerShell has to say about hash tables. Type in get-help about_Hash_Tables for more information.

SHORT DESCRIPTION
Describes how to create, use, and sort hash tables in Windows PowerShell.

LONG DESCRIPTION
A hash table, also known as a dictionary or associative array, is a
compact data structure that stores one or more name/value pairs. For
example, a hash table might contain a series of names and employee IDs,
computer names and IP addresses, or message IDs and message text.

Hash tables are frequently used because they are very efficient for finding
and retrieving data. You can use hash tables to store lists and to create
calculated properties in Windows PowerShell. And, Windows PowerShell has a
cmdlet, ConvertFrom-StringData, that converts strings to a hash table.

Hash Table Syntax:
@{ = ; [ = ] …}

Hash Table Guidelines:

  • Begin the hash table with an at sign (@).
  • Enclose the hash table in braces ({}).
  • Enter one or more name-value pairs for the content of the hash
  • table.
  • Use an equal sign (=) to separate each name from its value.
  • Use a semicolon (;) to separate the name/value pairs.
  • If a name or value contains spaces, enclose it in quotation marks.

How to Declare a Hash Table:

Static Example:
$table = @{v1=”Hello”;v2=”World”;v3=”!”;}

Or you could try creating this table from a string, here is what the string will look like for the example above.
> $string = @”
v1=”Hello”
v2=”World”
v3=”!”
“@

Use convertfrom-stringdata to convert a string into a hash table.
$table = convertfrom-stringdata $string

Advertisements

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