select object from a pipline and run it

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

  • Author
    Posts
  • #16911
    Profile photo of Garegin Asatryan
    Garegin Asatryan
    Participant

    Hello

    I had a quick question. How do you select an object from a pipeline? I'm guessing select-object, but I may be wrong. for example I run

    gcm *pssession

    after which I want to run some of the commands in a batch from that list (for example, all the ones that start with get*). is such a thing possible?

    Thank You

  • #16912
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Select-Object is designed to choose the properties that you want to display, from the properties supported by whatever's in the pipeline. "Select," in PowerShell terminology, doesn't mean "choose and execute."

    Get-Command doesn't return executable command objects per se; it returns information about a command. That information does include the commands' names, so I suppose you could use something like ForEach-Object and Invoke-Expression to do what you're asking. I'm not sure I understand the use case, though. I guess I'm not imagining where I would want to query a bunch of command names and then arbitrarily run them with no parameters.

  • #16916
    Profile photo of Garegin Asatryan
    Garegin Asatryan
    Participant

    actually, it was just a learning exercise with selecting objects. a more practical way to do it, would be to just copy paste the command name from the list

  • #16918
    Profile photo of Don Jones
    Don Jones
    Keymaster

    You might be thinking of Where-Object, which is designed to remove specified objects from the pipeline and pass along what's left. Select-Object is "select" the way SQL uses the term. You're not selecting objects, actually; you're selecting properties of objects. For every object input, you get an object out.

You must be logged in to reply to this topic.