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.
How foreach loop works? The in keyword used along with foreach loop is used to iterate over the iterable-item . The in keyword selects an item from the iterable-item on each iteration and store it in the variable element . On first iteration, the first item of iterable-item is stored in element.
There is no foreach loop in C, but both C++ and Java have support for foreach type of loop. In C++, it was introduced in C++ 11 and Java in JDK 1.5. 0 The keyword used for foreach loop is “for” in both C++ and Java.
Prerequisite: Loops in C# The foreach loop is used to iterate over the elements of the collection. The collection may be an array or a list. It executes for each element present in the array. It is necessary to enclose the statements of foreach loop in curly braces {}.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With