PowerShell -is operator always matching PSCustomObject when used in ForEach-Object












2














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?










share|improve this question


















  • 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
















2














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?










share|improve this question


















  • 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














2












2








2


1





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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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] 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














  • 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








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












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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.






    share|improve this answer




























      1














      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.






      share|improve this answer


























        1












        1








        1






        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 at 4:54

























        answered Nov 13 at 4:45









        mklement0

        126k20239267




        126k20239267






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python