When I started working with Power Shell 2 years ago I slowly but surely ...
I have found the following code below months ago online, but ever since I cannot find it anymore and therefore want to provide it here, so that it does not vanish from the internet. If I find the author or post again, I will post a link to it.
So, I ran into the situation that I had to redo my startup script (yes, you read that right) for my notebook that was provided by my employer and really needed a hash table where I could define property names and add multiple objects all at once, much like the output of “Get-Process”.
Therefore I took a look at both official resources at Everything you wanted to know about hashtables - PowerShell Microsoft Docs and Everything you wanted to know about PSCustomObject - PowerShell Microsoft Docs, and it turns out I need a custom object not a hash table. I could create a nested hash table but that would not really be the solution I was looking for. After some research on the web, I recalled that this happened to me before and I managed to pull out some old code from our git.
Right, this is already so much easier to read than all the other methods. But it is not really more human readable, so by simply adding brackets and a few lines breaks it transforms to something really easy to read.
So how does this all work? Well, something that is somewhat confusing at the first glance is that we add the objects in the desired order first and declare the property’s last.
So where did I end up using it in my startup script from earlier? Well, the hash table is being used as a list for the apps to launch. Which looks something like this:
So, kind of ironically the hash table became so big I must use the Out-GridView Cmdlet to show all its content. The goal is that the content of the table is readable in the editor and not to have to use any of the Import-* or ConvertFrom-* Cmdlets.
This is mainly intended for scripts where you do not want to load data from a file or another source in PowerShell but still need a proper object that you can edit easily.