Try - Catch differences in foreach loop

Welcome Forums General PowerShell Q&A Try - Catch differences in foreach loop

This topic contains 2 replies, has 2 voices, and was last updated by

5 months ago.

  • Author
  • #114489

    Points: 0
    Rank: Member

    Dear guys,

    I've notices strage behaviour of the "try-catch" function.

    For testing purposes I've stored to variable 2 random windows services with 2 properties – "name" and "displayname". Then I've changed the property "name" on the second object in array to value "something".

    $services= get-service | select -first 2 name,displayname



    Now, I want to achieve thru loop that when error appears, while trying to get service with the name "something" (in the "try" script block), it tries to get the service by property "displayname" (in catch scriptblock).


    $services |    foreach  {

    try {get-service $ -ErrorAction Stop} catch

    {get-service $_.displayname}


    The code above, failes to  get the service by the value stored in the "$_.displayname" because it is empty.. But in the code below it normally executes $s.displayname.

    foreach ($s in $services) {

    try {get-service $ -ErrorAction stop} catch

    {get-service $s.displayname}


    Can someone please explain why this is happening.




  • #114490

    Points: 1,628
    Helping Hand
    Rank: Community Hero

    Well, $_ is the notation for Object passed through between cmdlets in a pipeline. But when you use Try{} Catch{}, $_ in catch is the Error details where ever you place it.

    Try{Get-Process -Name NoProcess -ErrorAction Stop}
    Try{Get-Process -Name NoProcess -ErrorAction Stop}
    Catch{$_ | Get-Member}

    When you use foreach statement(not ForEach-Object cmdlet), each object is not identified using $_, but its having its own temp variable which we give, here it is $S.

  • #114495

    Points: 0
    Rank: Member

    Thanks for the explanation 🙂

The topic ‘Try - Catch differences in foreach loop’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort