Pipeline Variables

  • Hi All

    I am trying to understand why my filtering doesn't work here....

    I am new to Powershell

    Using dbatools

    This works:

    $t = get-dbaregserver
    $t | Where-Object {$_.group -eq "GroupName"}

    This does not filter the result:

    $p = get-dbaregserver
    $p | Where-Object {$p.group -eq "GroupName"}
  • Thanks for posting your issue and hopefully someone will answer soon.

    This is an automated bump to increase visibility of your question.

  • The reason it doesn't work is because the variable $p contains a list of all objects returned from get-dbaregserver.  Passing that object through the pipeline to where-object - is passing each object one at a time.

    The variable $p - since it is a container for the list of objects does not have a property named group.  And if it did - which group should be returned.

    Now - to reference the current object passing through the pipeline, you have to use the $_ reference.  That is why the first one works - because it is reference the current object in the pipeline.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply