Run a program in a ForEach loop

Both Jeffrery Hicks and slipsec are correct. Yank the double quotes off.

$_ or $_.fullname worked in my test script (below). YMMV with your custom-tool.

gci | % { c:\windows\notepad.exe $_.fullname }

or

gci | % { c:\windows\notepad.exe $_ }

If you still need quotes around the command path (say, if you've got spaces), just do it like this:

ls | % { &"C:\Working\tools\custom-tool.exe" $_.FullName }

Notice the use of & before the string to force PowerShell to interpret it as a command and not a string.


ls | %{C:\Working\tools\custom-tool.exe $_}

As each object comes down the pipeline the tool will be run against it. Putting quotes around the command string causes it to be... a string! The local variable "$_" it then likely doesn't know what to do with so pukes with an error.


I'm betting your tool needs the full path. The $_ is each file object that comes through the pipeline. You likely need to use an expression like this:

ls | %{C:\Working\tools\custom-tool.exe $_.fullname}