Dynamically and conditionally renaming Powershell object property names
Renaming object property names (in any language) is a necessary component of many ETL processes, but I like to avoid hard coding solutions in the event of schema changes, as well as wanting to write code that’s easily reusable and modular. Powershell and DAX table queries don’t play natively nice when it comes to column names. In Powershell, object property names with square brackets [ ] require iteration to be used in filters like Where-Object, or need to be referenced via the slower wildcard ? match technique. So, when dealing with a [PSCustomObject] of: The native solution to filter this doesn’t work: Object names with the [ ] characters also break Out-GridView, which in itself is deprecated however still very useful. So, in this example, I will demonstrate how to programmatically and performantly rename Powershell object note property names in a manner that does not hard code column names, and does not require row by row iteration of your final table to do! This is easily the most performant way you can achieve this. We are going to create a hash table of Name/Expression pairs to replace [ with ( and ] with ), and run this through Select-Object. We …
Read More