PowerShell primers

More PowerShell: Hash tables

Using arcane text-based syntax to achieve miraculous results

PowerShell primers

Show More
1 2 Page 2
Page 2 of 2

Using multiple properties with hash tables

You can also use multiple properties with 'Select-Object.' For instance, we can just include the 'Id' property with our table to get both the process ID and the transformed column. Just separate out the properties in the 'Select-Object' command with commas but no spaces.

Get-Process | Select-Object Id,@{Name = "The Name of the Process"; Expression = {$_.ProcessName}}

And that results in the following:

PowerShell hash tables figure 3

And you might already know how to fix that funky off-centered formatting. If you do not understand, that’s fine -- there is a built-in PowerShell command called 'Format-Table' that does a nice job of preparing all of the output into a nice, neat, easily readable table:

Get-Process | Select-Object @{Name = "The Name of the Process"; Expression = {$_.ProcessName}} | Format-Table -AutoSize

You can also use multiple hash tables. For instance, let's rewrite that ID column to call it "Process ID" and keep the other column renamed, too.

Get-Process | Select-Object @{Name = "Process ID"; Expression = {$_.Id}},@{Name = "The Name of the Process"; Expression = {$_.ProcessName}} | Format-Table -AutoSize

We can see how that comes out:

PowerShell hash tables figure 4

Pretty cool, huh? And now you see what I mean when I say the syntax is unforgiveable. But that is how PowerShell works, at least as the state of its art right now.

A practical application of hash tables: Creating Active Directory accounts from a list

When might this be useful, however? There are some commands, particularly those that import data, which need values named certain ways. But sometimes you might have data that you need to import into those commands that is named in a different way. For instance, let's say you have a file that has a list of new interns who need accounts on your Active Directory domain. This might be a CSV feed from your hiring system, so it looks something like this:

First Name,Last Name,Logon Name,Division




But when you go to try to create a new Active Directory Domain Services user account with PowerShell, you are confronted with the following problem: For the command that you will use, 'New-ADUser,' there are parameters for GivenName and Surname and SamAccountName and Department. That's all well and good, but your file has data for First Name, Last Name, Logon Name and Division.

We'll use a hash table, then, with 'Select-Object' to take care of renaming the headers in your CSV file to the names that 'New-ADUser' is expecting. Finally, we'll pipe that into 'New-ADUser' to finish up the job.

Import-CSV c:\interns.csv | Select-Object @{Name="Surname";Expression={$_."Last Name"}}, @{Name="GivenName";Expression={$_."First Name"}},

@{Name="SamAccountName";Expression={$_."Logon Name"}},


The last word

That was pretty in-the-weeds, wasn't it? But it's a very powerful capability and one you will surely use as you step into more advanced scripting and administration scenarios. And it shows off just how useful PowerShell output as objects can be.

Copyright © 2016 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2
7 inconvenient truths about the hybrid work trend
Shop Tech Products at Amazon