Author Posts

July 9, 2014 at 6:59 am

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

July 9, 2014 at 7:03 am

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.

July 9, 2014 at 7:25 am

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

July 9, 2014 at 7:27 am

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.