PowerShell -is operator always matching PSCustomObject when used in ForEach-Object
Using PowerShell Core 6.1 on Mac. It appears that piping an array to ForEach-Object is modifying or wrapping each element such that the -is operator considers all of them to be PSCustomObjects.
Let me demonstrate:
Set up an array of four items of different types (use of JSON because that's where the data is coming from in my real use case):
$a = '[4, "Hi", {}, true]' | ConvertFrom-Json
Iterate the list by index and determine which ones are PSCustomObjects:
0..3 | ForEach-Object {
$v = $a[$_]
$t = $v.GetType().FullName
$is = $v -is [PSCustomObject]
"$t - $is"
}
The output (for me) is exactly what I would expect:
System.Int64 - False
System.String - False
System.Management.Automation.PSCustomObject - True
System.Boolean - False
But if I just pipe the array to ForEach-Object:
$a | ForEach-Object {
$v = $_
$t = $v.GetType().FullName
$is = $v -is [PSCustomObject]
"$t - $is"
}
Now the output claims that all four are PSCustomObjects:
System.Int64 - True
System.String - True
System.Management.Automation.PSCustomObject - True
System.Boolean - True
Could anyone explain what is happening here?
powershell powershell-v6.0
add a comment |
Using PowerShell Core 6.1 on Mac. It appears that piping an array to ForEach-Object is modifying or wrapping each element such that the -is operator considers all of them to be PSCustomObjects.
Let me demonstrate:
Set up an array of four items of different types (use of JSON because that's where the data is coming from in my real use case):
$a = '[4, "Hi", {}, true]' | ConvertFrom-Json
Iterate the list by index and determine which ones are PSCustomObjects:
0..3 | ForEach-Object {
$v = $a[$_]
$t = $v.GetType().FullName
$is = $v -is [PSCustomObject]
"$t - $is"
}
The output (for me) is exactly what I would expect:
System.Int64 - False
System.String - False
System.Management.Automation.PSCustomObject - True
System.Boolean - False
But if I just pipe the array to ForEach-Object:
$a | ForEach-Object {
$v = $_
$t = $v.GetType().FullName
$is = $v -is [PSCustomObject]
"$t - $is"
}
Now the output claims that all four are PSCustomObjects:
System.Int64 - True
System.String - True
System.Management.Automation.PSCustomObject - True
System.Boolean - True
Could anyone explain what is happening here?
powershell powershell-v6.0
1
[PSCustomObject]
is an alias for[System.Management.Automation.PSObject]
, but not to[System.Management.Automation.PSCustomObject]
. So, that means that all four are wrapped into[PSObject]
insideForEach-Object
cmdlet. Likely becauseInputObject
parameter ofForEach-Object
typed as[PSObject]
andForEach-Object
does not do unwrapping.
– PetSerAl
Nov 12 at 19:43
add a comment |
Using PowerShell Core 6.1 on Mac. It appears that piping an array to ForEach-Object is modifying or wrapping each element such that the -is operator considers all of them to be PSCustomObjects.
Let me demonstrate:
Set up an array of four items of different types (use of JSON because that's where the data is coming from in my real use case):
$a = '[4, "Hi", {}, true]' | ConvertFrom-Json
Iterate the list by index and determine which ones are PSCustomObjects:
0..3 | ForEach-Object {
$v = $a[$_]
$t = $v.GetType().FullName
$is = $v -is [PSCustomObject]
"$t - $is"
}
The output (for me) is exactly what I would expect:
System.Int64 - False
System.String - False
System.Management.Automation.PSCustomObject - True
System.Boolean - False
But if I just pipe the array to ForEach-Object:
$a | ForEach-Object {
$v = $_
$t = $v.GetType().FullName
$is = $v -is [PSCustomObject]
"$t - $is"
}
Now the output claims that all four are PSCustomObjects:
System.Int64 - True
System.String - True
System.Management.Automation.PSCustomObject - True
System.Boolean - True
Could anyone explain what is happening here?
powershell powershell-v6.0
Using PowerShell Core 6.1 on Mac. It appears that piping an array to ForEach-Object is modifying or wrapping each element such that the -is operator considers all of them to be PSCustomObjects.
Let me demonstrate:
Set up an array of four items of different types (use of JSON because that's where the data is coming from in my real use case):
$a = '[4, "Hi", {}, true]' | ConvertFrom-Json
Iterate the list by index and determine which ones are PSCustomObjects:
0..3 | ForEach-Object {
$v = $a[$_]
$t = $v.GetType().FullName
$is = $v -is [PSCustomObject]
"$t - $is"
}
The output (for me) is exactly what I would expect:
System.Int64 - False
System.String - False
System.Management.Automation.PSCustomObject - True
System.Boolean - False
But if I just pipe the array to ForEach-Object:
$a | ForEach-Object {
$v = $_
$t = $v.GetType().FullName
$is = $v -is [PSCustomObject]
"$t - $is"
}
Now the output claims that all four are PSCustomObjects:
System.Int64 - True
System.String - True
System.Management.Automation.PSCustomObject - True
System.Boolean - True
Could anyone explain what is happening here?
powershell powershell-v6.0
powershell powershell-v6.0
asked Nov 12 at 18:55
user221592
432
432
1
[PSCustomObject]
is an alias for[System.Management.Automation.PSObject]
, but not to[System.Management.Automation.PSCustomObject]
. So, that means that all four are wrapped into[PSObject]
insideForEach-Object
cmdlet. Likely becauseInputObject
parameter ofForEach-Object
typed as[PSObject]
andForEach-Object
does not do unwrapping.
– PetSerAl
Nov 12 at 19:43
add a comment |
1
[PSCustomObject]
is an alias for[System.Management.Automation.PSObject]
, but not to[System.Management.Automation.PSCustomObject]
. So, that means that all four are wrapped into[PSObject]
insideForEach-Object
cmdlet. Likely becauseInputObject
parameter ofForEach-Object
typed as[PSObject]
andForEach-Object
does not do unwrapping.
– PetSerAl
Nov 12 at 19:43
1
1
[PSCustomObject]
is an alias for [System.Management.Automation.PSObject]
, but not to [System.Management.Automation.PSCustomObject]
. So, that means that all four are wrapped into [PSObject]
inside ForEach-Object
cmdlet. Likely because InputObject
parameter of ForEach-Object
typed as [PSObject]
and ForEach-Object
does not do unwrapping.– PetSerAl
Nov 12 at 19:43
[PSCustomObject]
is an alias for [System.Management.Automation.PSObject]
, but not to [System.Management.Automation.PSCustomObject]
. So, that means that all four are wrapped into [PSObject]
inside ForEach-Object
cmdlet. Likely because InputObject
parameter of ForEach-Object
typed as [PSObject]
and ForEach-Object
does not do unwrapping.– PetSerAl
Nov 12 at 19:43
add a comment |
1 Answer
1
active
oldest
votes
PetSerAl, as he frequently does, has provided the crucial pointer in a comment:
Piping objects to ForEach-Object
wraps them in a [psobject]
instance, which causes -is [pscustomobject]
/ -is [psobject]
to return $True
for any input object, because - confusingly - [pscustomobject]
is the same as [psobject]
: they're both type accelerators for [System.Management.Automation.PSObject]
- against what one would expect,[pscustomobject]
is not short for [System.Management.Automation.PSCustomObject]
.
Therefore, test the input objects for being instances of [System.Management.Automation.PSCustomObject]
rather than [pscustomobject]
:
$a | ForEach-Object {
$_ -is [System.Management.Automation.PSCustomObject]
}
Note that if you use a foreach
loop, even -is [pscustomobject]
would work, because the objects being enumerated are then not wrapped in an extra [psobject]
instance:
foreach ($element in $a) {
$element -is [pscustomobject]
}
This works, because even a bona fide [System.Management.Automation.PSCustomObject]
is technically also a [System.Management.Automation.PSObject]
behind the scenes.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268394%2fpowershell-is-operator-always-matching-pscustomobject-when-used-in-foreach-obje%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
PetSerAl, as he frequently does, has provided the crucial pointer in a comment:
Piping objects to ForEach-Object
wraps them in a [psobject]
instance, which causes -is [pscustomobject]
/ -is [psobject]
to return $True
for any input object, because - confusingly - [pscustomobject]
is the same as [psobject]
: they're both type accelerators for [System.Management.Automation.PSObject]
- against what one would expect,[pscustomobject]
is not short for [System.Management.Automation.PSCustomObject]
.
Therefore, test the input objects for being instances of [System.Management.Automation.PSCustomObject]
rather than [pscustomobject]
:
$a | ForEach-Object {
$_ -is [System.Management.Automation.PSCustomObject]
}
Note that if you use a foreach
loop, even -is [pscustomobject]
would work, because the objects being enumerated are then not wrapped in an extra [psobject]
instance:
foreach ($element in $a) {
$element -is [pscustomobject]
}
This works, because even a bona fide [System.Management.Automation.PSCustomObject]
is technically also a [System.Management.Automation.PSObject]
behind the scenes.
add a comment |
PetSerAl, as he frequently does, has provided the crucial pointer in a comment:
Piping objects to ForEach-Object
wraps them in a [psobject]
instance, which causes -is [pscustomobject]
/ -is [psobject]
to return $True
for any input object, because - confusingly - [pscustomobject]
is the same as [psobject]
: they're both type accelerators for [System.Management.Automation.PSObject]
- against what one would expect,[pscustomobject]
is not short for [System.Management.Automation.PSCustomObject]
.
Therefore, test the input objects for being instances of [System.Management.Automation.PSCustomObject]
rather than [pscustomobject]
:
$a | ForEach-Object {
$_ -is [System.Management.Automation.PSCustomObject]
}
Note that if you use a foreach
loop, even -is [pscustomobject]
would work, because the objects being enumerated are then not wrapped in an extra [psobject]
instance:
foreach ($element in $a) {
$element -is [pscustomobject]
}
This works, because even a bona fide [System.Management.Automation.PSCustomObject]
is technically also a [System.Management.Automation.PSObject]
behind the scenes.
add a comment |
PetSerAl, as he frequently does, has provided the crucial pointer in a comment:
Piping objects to ForEach-Object
wraps them in a [psobject]
instance, which causes -is [pscustomobject]
/ -is [psobject]
to return $True
for any input object, because - confusingly - [pscustomobject]
is the same as [psobject]
: they're both type accelerators for [System.Management.Automation.PSObject]
- against what one would expect,[pscustomobject]
is not short for [System.Management.Automation.PSCustomObject]
.
Therefore, test the input objects for being instances of [System.Management.Automation.PSCustomObject]
rather than [pscustomobject]
:
$a | ForEach-Object {
$_ -is [System.Management.Automation.PSCustomObject]
}
Note that if you use a foreach
loop, even -is [pscustomobject]
would work, because the objects being enumerated are then not wrapped in an extra [psobject]
instance:
foreach ($element in $a) {
$element -is [pscustomobject]
}
This works, because even a bona fide [System.Management.Automation.PSCustomObject]
is technically also a [System.Management.Automation.PSObject]
behind the scenes.
PetSerAl, as he frequently does, has provided the crucial pointer in a comment:
Piping objects to ForEach-Object
wraps them in a [psobject]
instance, which causes -is [pscustomobject]
/ -is [psobject]
to return $True
for any input object, because - confusingly - [pscustomobject]
is the same as [psobject]
: they're both type accelerators for [System.Management.Automation.PSObject]
- against what one would expect,[pscustomobject]
is not short for [System.Management.Automation.PSCustomObject]
.
Therefore, test the input objects for being instances of [System.Management.Automation.PSCustomObject]
rather than [pscustomobject]
:
$a | ForEach-Object {
$_ -is [System.Management.Automation.PSCustomObject]
}
Note that if you use a foreach
loop, even -is [pscustomobject]
would work, because the objects being enumerated are then not wrapped in an extra [psobject]
instance:
foreach ($element in $a) {
$element -is [pscustomobject]
}
This works, because even a bona fide [System.Management.Automation.PSCustomObject]
is technically also a [System.Management.Automation.PSObject]
behind the scenes.
edited Nov 13 at 4:54
answered Nov 13 at 4:45
mklement0
126k20239267
126k20239267
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268394%2fpowershell-is-operator-always-matching-pscustomobject-when-used-in-foreach-obje%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
[PSCustomObject]
is an alias for[System.Management.Automation.PSObject]
, but not to[System.Management.Automation.PSCustomObject]
. So, that means that all four are wrapped into[PSObject]
insideForEach-Object
cmdlet. Likely becauseInputObject
parameter ofForEach-Object
typed as[PSObject]
andForEach-Object
does not do unwrapping.– PetSerAl
Nov 12 at 19:43