Powershell – Manipulating objects to pass them along the pipeline

Before getting into this, I’d like to discuss the pipeline a bit. In the pipeline you have to ways of passing objects, by value (typename) or by property name (which is powershell’s second try)

 

For example, let’s see what kind of objects get-process creates by piping get-member to it (I use the alias GM)

 

Now you could pipe objects from get-process to stop-process because stop-process takes input objects by the typename system.diagnostics.process.

We can verify this by simply reading the full help.

 

Now if we did get-process -name notepad | stop-process

The object notepad which is of the type system.diagnostics.process get’s piped into stop-process, it gets piped into this parameter, which takes input by value.

Notice how in the syntax below we can see that inputobject is optional, because it’s a placeholder for process, which takes multiple objects (I know this because it says [] inside of process, which means multiple objects…

So now if we want to know what would happen if passed the object to stop-process, we would run a -whatif

Now stop-process has parameters for doing this much faster, however I find that using this command makes it much easier to explain what is happening and how objects are being passed through the pipeline.

 

 

The above command would accomplish the same thing, however it uses a parameter to filter the objects rather then getting them from the pipeline. Which makes the above command much faster to run than piping objects into stop-process.

(powershell tip : formatting should always be kept to the left as much as possible).

 

Now I’m going to show you how we can accomplish the same things but instead this time we will pass objects of the type system.string and powershell will match them via their propertyname and input them into the parameter that takes input from pipeline by property name.

 

 

I notice in the help file that -name takes input from the pipeline by property name. Now you should know that properties are the column headers in collections (collections are a set of objects)

In the above screenshot we see that the column header is name, and this is a property. So now recall that -name from stop-process takes input from pipeline by propertyname. It just so happens that I created a hashtable with a property of name, and now the parameter and the property match! Now powershell can see this connection and take this input!

 

I created this hashtable with this command…

 

In the above I created a custom powershell object, then created a hash table, the property is the first value you see ‘name’, and the second string is what’s put into the rows under the column of the property name. So this single object above has a value of ‘notepad’ and a property of name.

The same can be accomplished with

Now since we have a hash table with the property ‘name’ powershell can reference this and put it into stop-process’ -name parameter, and since it finds objects with that property name, it will try to stop them!

BAM!!!

Now you know how to manipulate strings and objects to force them into parameters in the next command via the pipeline!

Leave a comment

Exit mobile version