Pipeline and non-pipelined input?

This topic contains 5 replies, has 2 voices, and was last updated by Profile photo of BerndNowak BerndNowak 3 years, 2 months ago.

  • Author
  • #9632
    Profile photo of BerndNowak

    Hi all,
    I'm new to powershell so maybe it's easy.

    I have a parameter ComputerName which has the following parameter properties:

    # Computer Name oder mehrere Computer Namen

    Now I learnt (I do hope that I get this right) that when passing objects via the pipeline the process block will be automatically looped for every object coming from the pipeline. Do I have this right?

    However, when I use get-something -ComputerName client1, client2 etc I need to loop through the "array" of $ComputerName.

    Can I determine in the Begin block if the script is used as a pipeline "target"?


  • #9633
    Profile photo of Don Jones
    Don Jones

    Correct. When input is piped in, $Computername will only contain one item at a time. If provided on the parameter, PROCESS executes once and $ComputerName contains all items.

    You don't need to determine it, though. Just build the PROCESS block to handle both.

     foreach($computer in $computername) {
       # use $computer

    In the pipeline mode, the foreach is redundant but doesn't add much extra time since it only has to loop once. Either way, $computer will only contain one name at a time, this way.

  • #9636
    Profile photo of BerndNowak

    Took me some time from home to further explain my problem and maybe I have found some info for myself.

    If I use this command:

    $myVar = Get-ADComputer -Filter {OperatingSystem -NotLike "*server*"} -Properties * | select Name

    The content of $myVar | gm shows this:

    ypeName: Selected.Microsoft.ActiveDirectory.Management.ADComputer

    Name MemberType Definition
    —- ———- ———-
    Equals Method bool Equals(System.Object obj)
    GetHashCode Method int GetHashCode()
    GetType Method type GetType()
    ToString Method string ToString()
    Name NoteProperty System.String Name=SomeName

    So a piped Get-ADComputer -Filter {OperatingSystem -NotLike "*server*"} -Properties * | select Name still has something like "Name=SomeName" in it.
    Doing a $myVar[-1].Name | gm will give me only the string name and not "Name=SomeName".

    What would I need to change either in the process code block or in the calling pipe command?

    Get-ADComputer -Filter {OperatingSystem -NotLike "*server*"} -Properties * | select Name | something | myscript

    Thanks a lot because I now understand that foreach loop and why it would still be functioning.

    Oh, here's what I did:

    Get-ADComputer -Filter {OperatingSystem -NotLike "*server*"} -Properties * |Select -ExpandProperty Name| myscript
    Is this the right way?

  • #9638
    Profile photo of Don Jones
    Don Jones

    When you use Select-Object, you're still producing a complex object with properties, as you've seen.

    Get-ADComputer -filter * | Select -Expand Name | My-Function

    Is one option. Extract (expand) the contents of the Name property so that it hits your function ByValue. Or,

    Get-ADComputer -filter * | Select @{n='computername';e={$_.name}} | My-Function

    Produces an object having only a ComputerName property, which hits your function ByPropertyName.

    All in "Learn Windows PowerShell 3 in a Month of Lunches," if you're interested :).

  • #9645
    Profile photo of BerndNowak

    Thanks a lot 🙂

  • #9646
    Profile photo of BerndNowak

    It was late yesterday and now I could see the ByValue and ByPropertyName!
    Will check this one: Get-ADComputer -filter * | Select @{n=’computername’;e={$_.name}} | My-Function

    By the way, couldn't resist buying the book. I have watched the 2 introduction events from Jeffrey Snower and Jason Helmick and for sure this was mentioned 🙂

    Many more questions for new threads but I may solve some on my own 😉

You must be logged in to reply to this topic.