I recently came across a situation where I wanted to group a custom object based on a property that contained a question mark.
[code language=”powershell” light=”true”] [PSCustomObject]@{“test?”=”test”} | group ‘test?’ [/code]
Running the above resulted in the error message “group : Wildcard characters are not allowed in “test?”.”. To make Group-Object work with the property name that contains a wildcard character the property name needs to be escaped either manually by preceding the wildcard character by “" (e.g. "test
?”) or utilizing the related .net method.
[code language=”powershell” light=”true”] [PSCustomObject]@{“test?”=”test”} | group [Management.Automation.WildcardPattern]::Escape(‘test?’)) [/code]
photo credit: the tree II via photopin (license)