How to pass the current powershell pipe-object “$_” as a command line arg?

Question:

I’m trying to set all svn:property’s on a set of piped files:

I get following error:

The problem is, that $_ is not passed onto svn propset…

What to do?

Answer:

For $_ to have a value you have to use it in a context where it is actually set. For your specific scenario this means that you have to wrap your call to svn into a ForEach-Object cmdlet:

(I have used the % alias here for brevity)

Inside that ForEach the variable $_ has a value and can be used.

However, I have seen some uses where the current pipeline object got appended to a program’s arguments when piping into non-cmdlets. I haven’t fully understood that so far, though.

But to understand what you are doing here: You are trying to set svn:keywords on every file which uses one of those. A probably more robust and readable approach would be to actually filter the list you are scanning:

Also, here you can access all properties of the file object and not need to rely on the object Select-String gives you.

Source:

How to pass the current powershell pipe-object "$_" as a command line arg? by licensed under CC BY-SA | With most appropriate answer!

}
(might work, haven’t tested)

You can then continue by just piping that into a foreach:

Also, here you can access all properties of the file object and not need to rely on the object Select-String gives you.

Source:

How to pass the current powershell pipe-object “$_” as a command line arg? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply