2

I know there are several questions and answers about this - I tried them all - none of them worked for me.

How do I do the equivalent of this (which works in cmd) in a PowerShell command?

for %f in (*.mp4) do "C:\Program Files\DVR-Scan\dvr-scan.exe" -i %f -d events/ -tc

I tried this:

Get-ChildItem -File | Foreach {"C:\Program Files\DVR-Scan\dvr-scan.exe" -i $_.fullname -d events/ -tc }

And like this:

Get-ChildItem -File | Foreach {"C:\Program Files\DVR-Scan\dvr-scan.exe -i $_.fullname -d events/ -tc "}

And like this:

ls -file | & { "C:\Program Files\DVR-Scan\dvr-scan.exe -i $_.fullname -d events/ -tc " }    

So what's the right way to do it?

EDIT: This question is not a duplicate of this one. In the other question they are asking about the equivalent of a Bash command. I am asking about the equivalent of a cmd command. Even if the answers are the same, it is not the same question.

traveh
  • 2,700
  • 3
  • 27
  • 44
  • 2
    The first example you have on pwsh looks perfect but you just need the call operator `&` before the path: `Foreach { & "C:\Program.....` – Santiago Squarzon Dec 20 '22 at 21:44
  • You can also use the [`Start-Process` cmdlet](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-7.3) (`saps` or `start` for short) to make it a bit more explicit, which may also make it a bit easier to understand what's going on to others that might look at it later on. – Jesse Dec 20 '22 at 21:54
  • @Jesse, to synchronously execute console applications or batch files and capture their output, call them _directly_ (`c:\path\to\some.exe ...` or `& $exePath ...`), do _not_ use `Start-Process` (or the `System.Diagnostics.Process` API it is based on) - see [this answer](https://stackoverflow.com/a/51334633/45375). [GitHub docs issue #6239](https://github.com/MicrosoftDocs/PowerShell-Docs/issues/6239) provides guidance on when use of `Start-Process` is and isn't appropriate. – mklement0 Dec 20 '22 at 22:27
  • In short, as implied by Santiago's comment: An executable path that is _quoted_ or contains _variable references_ must - for syntactic reasons - be invoked with `&`, the [call operator](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Operators#call-operator-); see the linked duplicate for details. – mklement0 Dec 20 '22 at 22:29
  • @mklement0 The OP didn't say they needed the output, and to run it synchronously you can use the `-Wait` flag. If you don't want it to open a new window, use the `-NoNewWindow` flag. There are options. – Jesse Dec 20 '22 at 22:39
  • 1
    @Jesse, the OP asked for an equivalent to the batch-file command given. `Start-Process` cannot provide that equivalent (`-NoNewWindow` makes the process run in the same console, but you won't be able to _capture or redirect_ its output). `Start-Process` has its place, but only in very specific contexts, as explained in the [previously linked issue](https://github.com/MicrosoftDocs/PowerShell-Docs/issues/6239). – mklement0 Dec 20 '22 at 22:44

0 Answers0