Search code examples
powershellforeachpipeline

Run a program in a ForEach loop


I'm trying to get this simple PowerShell script working, but I think something is fundamentally wrong. ;-)

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

I basically want to get files in a directory, and pass them one by one as arguments to the custom tool.


Solution

  • 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.