Today I would like to share “creative” way of abusing dynamic parameters in PowerShell I came up with yesterday. All started with challenging question on PowerShell TechNet forum. But before I get there I would like to show also why and how you would use them normally, so that you know why I would call my trick an “abuse”. All examples will be pretty naive and not production-oriented. For better examples – lookup poshcode, especially Joel’s and Oisin’s magical tools. Few to start with:
It could probably be just yet another comment under “What you see is NOT what you get” post. But I’ve decided I would give it more space and make code more readable than it’s possible in comments.
As I said before: for me nice output is less important than data itself. If I can keep data and make it look nice – I’m more than OK with that. I spent most of my time in PowerShell window so nice output make me happy camper. That’s why whenever I write a module I spent some extra time on it to add ps1xml files to it and decide how both Format-List and Format-Table should behave with objects I create. Obviously, objects also have some custom names, usually they have some pseudo-type tattoo rather than type wrote in C# and added to PowerShell with Add-Type.
Problem with more ad-hoc scripting (or scripting in general if module is not an option) in version 2 of PowerShell is that you can’t easily create this custom formatting on the fly. Building ps1xml file is overkill in such scenario, and sending strings to the pipe is something I would rather avoid if original data is something of a different nature (numeric, DateTime, etc.). So, what I would do?