Please feel free to provide feedback or file bugs here.

Convertfrom-json & Foreach-Object Operation vs ScriptBlock statement returns different output

Beginning in Windows PowerShell 3.0, there are two different ways to construct a ForEach-Object command. - Yes and no.

PS > $p
[
{
"Name": "aaCenter",
"Id": 4256
},
{
"Name": "AiNap",
"Id": 3408
},
{
"Name": "AllShareDMS",
"Id": 5044
}
]

# Blank result
$p | ConvertFrom-Json | % Name

#ScriptBlock or invoke GetEnumerator() method works as expected
PS > ($p | ConvertFrom-Json).GetEnumerator() | % Name
aaCenter
AiNap
AllShareDMS

PS > $p | ConvertFrom-Json | % {$_.Name}
aaCenter
AiNap
AllShareDMS

PS > ($p | ConvertFrom-Json).foreach{$_.Name}
aaCenter
AiNap
AllShareDMS

# Object doesn't contain a property Name
PS > $p | ConvertFrom-Json | Get-Member -Name Name

Ps. It will be good to correct a documentation about behavior.

1 vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)

    We’ll send you updates on this idea

    AlexanderAlexander shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    0 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...

      Feedback and Knowledge Base