Why work so hard with hashtables?

Recently I’ve see hashtables all around: blog posts, videos, webcasts. And every time I see it I ask myself simple question: why people tend to work too hard when defining those? I mean technicaly:

  1. $MyHash = @{ ‘key’ = ‘value’;
  2.     ‘nextkey’ = ‘othervalue’ }

is correct syntax to define hashtable. But I ask myself instantly: why? I can compare it with writing few lines of code like that:

  1. Get-ChildItem ‘.\’;
  2. GetQADUser ‘DoeJ’;

For some reason I would guess nobody (once he know he does not have to) would not write code like that. If command expects string, and it contains only ‘good’ characters you don’t have to quote it. Also: if you need to write few commands, each in separate line, you do not need to finish each line with semicolon. The only reason why semicolon is used when defining hashtables is because it’s technically similar to having few lines of code in one line. Same for key names: if it contains something wacky – please, use single quotes, or it may fail. If not – why bother? So my example hashtable, when defined in few lines, can be easily done without few quotes and without any semicolon:

  1. $MyHash = @{ key = ‘value’
  2.     nextkey = ‘othervalue’ }

I honestly like PowerShell for that it knows usually what I want. It saves some time here, when I define $MyHash. But it save a lot of time when you don’t have to quote each string, and separate each line of code with semicolon.


4 thoughts on “Why work so hard with hashtables?

  1. Thanks for the post. But can you explain how to get variables into hash tables?

    #Function to read the output from #the last backup logs and #determine the result and show it
    function Get-Backuplog {
    $backup = Get-ChildItem “C:\data”
    foreach ($file in $backup) {
    switch ($file){
    {Select-String $file -Pattern “Successful”}{Write-Host $file.Name was successful $file.LastAccessTime}
    {Select-String $file -Pattern “Warning”} {Write-Host $file.Name had warnings $file.LastAccessTime}
    {Select-String $file -Pattern “Failed”} {Write-Host $file.Name failed $file.LastAccessTime}
    default {Write-Host $file.Name had unknown backupstate $file.LastAccessTime}

    I have tried to put this into a hash table but whenever I put ” or “” the variables are not being populated correctly in the output any help would be much appreciated. Thanks

  2. As mentioned already on twitter: problem of displaying properties of objects in quotes is rooted in order of execution: $variable is being extended first (using it’s .ToString() method) and .Property is just another string to add.. You need to change order using subexpression $() – it will force powershell to read property and use it’s .ToString() method instead. 🙂

  3. Thanks for comment, mjolinor. 🙂 I agree that for hash that are build of strings (as those in my samples) your method is probably the easiest one. But when you need something more complicated (as hashtables for format-* and select, or -Property @{} with nested objects) it’s failing badly. Anyway: my point was that if you have to build hashtable and you already use several lines for that, than you can leave semicolons alone. Same as you can when writing few commands, each in separate line… 🙂

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