'Powershell Where-Object CmdLet with -Cin option not giving the expected result
I am trying to filter the items in a PSCustomobject with Where-Object CmdLet. At first I tried to filter the contents of the PSCustomobject Object with Where-Object CmdLet with -Ccontains option as show below...
$a1 = [PSCustomObject]@(
[PSCustomObject]@{
k1 = @('password', 1111)
k2 = 'x'
}
[PSCustomObject]@{
k1 = @('password', 2222)
k2 = 'x'
}
[PSCustomObject]@{
k1 = @('password', 3333)
k2 = 'x'
}
[PSCustomObject]@{
k1 = @('password', 4444)
k2 = 'x'
}
[PSCustomObject]@{
k1 = @('username', 1111)
k2 = 'y'
}
[PSCustomObject]@{
k1 = @('username', 2222)
k2 = 'y'
}
[PSCustomObject]@{
k1 = @('username', 3333)
k2 = 'y'
}
[PSCustomObject]@{
k1 = @('username', 4444)
k2 = 'y'
}
)
$a1_filtered = $a1 | Where-Object -Property 'k1' -Ccontains -Value 'password'
Write-Host $a1_filtered
The above code worked and I am getting the expected result and is shown below...
@{k1=System.Object[]; k2=x} @{k1=System.Object[]; k2=x} @{k1=System.Object[]; k2=x} @{k1=System.Object[]; k2=x}
But when I tried to achieve the same result with Where-Object CmdLet with the -Cin option as shown in the following code, I am getting empty result.
$a1 = [PSCustomObject]@(
[PSCustomObject]@{
k1 = @('password', 1111)
k2 = 'x'
}
[PSCustomObject]@{
k1 = @('password', 2222)
k2 = 'x'
}
[PSCustomObject]@{
k1 = @('password', 3333)
k2 = 'x'
}
[PSCustomObject]@{
k1 = @('password', 4444)
k2 = 'x'
}
[PSCustomObject]@{
k1 = @('username', 1111)
k2 = 'y'
}
[PSCustomObject]@{
k1 = @('username', 2222)
k2 = 'y'
}
[PSCustomObject]@{
k1 = @('username', 3333)
k2 = 'y'
}
[PSCustomObject]@{
k1 = @('username', 4444)
k2 = 'y'
}
)
$a1_filtered = $a1 | Where-Object -Value 'password' -Cin -Property 'k1'
Write-Host $a1_filtered
Please help me out on this.
Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
