DRY (Don’t repeat yourself) principal

Admit it, we’re all guilty of this at some point. There’s no shame in it, but if you persist in your ways once it has been brought to your attention…

I had a WET (We Enjoy Typing, get your mind out of the gutter!) moment recently. It had to do with getting the header from a csv File. Here’s where a little knowledge can derail real quick. Headers in a csv file are the first line, right? So get the first line of the csv file, do a -split ‘delimeter’ and I’m good to go right? Nothing wrong with the logic, but see, I’ll still be doing an import-csv later on… DRY!

Instead of getting the header ahead, why not get it after I’ve done the import? How? Why thank you for asking… 🙂

#region: csvLocation
$csvLocation =
1X,Elmstreet 26,1666 XX,NightMare
2X,PeterPan 01,0001 ZT,Neverland
3x,Mulan 5, 1111 TK, Disney
"@ |
ConvertFrom-Csv -Delimiter ','

$header = ($csvLocation | Get-Member | Where-Object {$_.membertype -eq 'noteproperty'}).name

I got that one on FB PowerShell group. It’s rapidly becoming my favorite social media hub for everything PowerShell related.

Here’s another: Lookup CSV row by value

This has helped me many a times in the past.

#region Group as a hashtable by 'LocationCode'
$lookupLocationCode = $csvLocation | Group-Object -AsHashTable -Property 'LocationCode'

Now if I’m looking for a CSV row by LocationCode value that’s pretty straightfoward. At least that’s what I thought… DRY!

Here’s the better option:

$csvLocation.Where({$_.LocationCode -eq '1x'})

LocationCode        StraatNaam          PostCode            Stad
------------        ----------          --------            ----
1X                  Elmstreet 26        1666 XX             NightMare

With this option I can even add a lil’ extra say :

$csvLocation.Where({($_.LocationCode -eq '1x') -and ($_.Stad -eq 'NigtMare')  })

#Oops a 'typo' no result, let's retry...
$csvLocation.Where({($_.LocationCode -eq '1x') -and ($_.Stad -eq 'NightMare')  })

#Ah... Better...
LocationCode        StraatNaam          PostCode            Stad
------------        ----------          --------            ----
1X                  Elmstreet 26        1666 XX             NightMare

With group-object as hashtable I need to select the key beforehand, this is way better! Thanks Hey, Scripting guy!

Know your cmdlets!

So this one is kinda embarassing. I had an old function lying around to ping in PowerShell back in the days before Test-Connection. Thing is, I was still using it. Michaja van der Zouwen pointed out to me that there’s a cmdlet for that… “Oh ok, Thanks!” And as luck would have it, at the PowerShell Summit, Don Jones pointed out that if you’re using ping instead of Test-Connection then your’e doing it wrong! I laughed “like a farmer with a toothache (Denglish)” Hehe… English equivalent: A phony laugh. Ok it wasn’t ‘that’ phony… Take away: Don’t reinvent the wheel… That’s enough shaming for one day…

Whatever your present/future endeavour, think DRY! You won’t regret it…

Hope it’s worth something to you,




4 thoughts on “DRY (Don’t repeat yourself) principal

  1. Dirk

    Hi Irwin,
    yeah, I also try to apply DRY, but it doesn’t always work out ;-).
    I would like to share two thoughts with you in relation to the post, hopefully this will be useful for you.
    1. $header = ($csvLocation | Get-Member | Where-Object {$_.membertype -eq ‘noteproperty’}).name could be shortened to
    $header = ($csvLocation | Get-Member -MemberType NoteProperty).Name following the principle to always filter as early as possible.
    2. It’s generally recommend to use the -AsString switch in conjunction with the -AsHashTable switch on Group-Object, otherwise you might run into situations where the keys of the generated hashtable are not strings which can in turn cause problems trying to find elements in the hashtable.
    When doing a lot of lookups within the same set of data there is not much than can beat a hashtable in terms of performance. but creating the hashtable in the first place has definitely also a cost.7



    Liked by 1 person

    1. Irwin Strachan Post author

      Hi Dirk,

      A quick update, group-object -AsHashTable is definitely waayyyy faster!!!I tried doing a lookup using .where but no… Took too long… I started another instance imported a csv with about 18000 rows, did a group-object -ashashtable, imported the second csv (3300 rows) and was finished before the other ISE Instance!!! Lesson learned…




  2. Pingback: Add Members to Group – a different approach | pshirwin

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 )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s