I want to iterate over folders and execute a command per folder, ignoring the already processed ones.
For this I'm trying to concatenate variables to produce a command and then execute this string.
But PowerShell (v7.2.5) is removing the variables from concatenation (or replacing them with empty strings).
I tried many different syntaxes like $($var1) + $($var2) or "$var1 $var2", none of them work. My current approach (it will run with Invoke-Expression -Command instead of echo):
$arguments = "-a -b -c"
$exe = "C:\foo\bar.exe"
$targetdir = "C:\path\"
Get-ChildItem $targetdir -Directory | ForEach-Object -Parallel {If($_.FullName.Contains("processed")){continue}; echo ("{0} -d {1} {2}" -f $($exe),$($_.FullName),$($arguments));} -ThrottleLimit 8
Expected:
C:\foo\bar.exe -d C:\path\101 -a -b -c
C:\foo\bar.exe -d C:\path\102 -a -b -c
C:\foo\bar.exe -d C:\path\103 -a -b -c
Output:
-d C:\path\101
-d C:\path\102
-d C:\path\103
Why is PowerShell removing paths or arguments from the concatenation and how do I fix this?