|
PoweShell 2.0. I just hit a 'gotcha' with Get-PathVariable. I have been using Add-PathVariable for a while to build my tools. But when I recently switched versions of my tools I wanted to remove the old version and add the new one.
I hit this gotcha: the standard way of 'filtering' elements out of arrays is to pipe the array, use '?' operator to filter values, then get the new list with values removed:
@('hello', 'codeplex') | ? { @('hello', 'world') -notconatins $_ } # this will give you the elements in the first array that aren't in the second array
But because Get-PathVariables returns [System.String[]] PowerShell doesn't iterate it by default:
Get-PathVariable | ? { $_ -notmatch 'SDK' } #trying to remove paths that contain 'SDK' but fails ... pipeline value isn't [string], it is [string[]]
There is an easy fix:
Get-PathVariable |
% { $_ } | # get [string[]], but send [string]
? { $_ -notmatch 'SDK' } # now filter on string
So, I'm wondering, is there a good reason to keep Get-PathVariable returning [string[]], or would it be best if this 'gotcha' was removed and return a pipeline-friendly array or list type?
|