Fundamentally, you invoke console applications the same way in PowerShell as you do in cmd.exe
, but there are important differences:
# If you really want to emulate `@echo ON` - see comments below.
Set-PSDebug -Trace 1
# * PowerShell doesn't support `<` for *input* redirection, so you must
# use Get-Content to *pipe* a file's content to another command.
# * `>` for *output* redirection *is* supported, but beware encoding problems:
# * Windows PowerShell creates a "Unicode" (UTF-16LE) file,
# * PowerShell (Core, v6+) a BOM-less UTF-8 file.
# * To control the encoding, pipe to Out-File / Set-Content with -Encoding
# * For syntactic reasons, because your executable path is *quoted*, you must
# invoke it via `&`, the call operator.
Get-Content "C:\..\CleanupInputs.txt" |
& "C:\...\cmdbdiag.exe" -u test -p test -s remedyar -t 41900 > "C:\...\DailyOutput.log"
# Turn tracing back off.
Set-PSDebug -Trace 0
exit 0
Note:
See also:
Re execution tracing: use of @echo ON
in your batch file and how it compares to PowerShell's
Set-PSDebug
-Trace 1
:
- Batch files typically run with
@echo OFF
so as not to echo every command itself before printing its output.
@echo ON
(or omitting an @echo ON/OFF
statement altogether) can be helpful for diagnosing problems during execution, however.
Set-PSDebug -Trace 1
is similar to @echo ON
, but it has one disadvantage: the raw source code of commands is echoed, which means that you won't see the value of embedded variable references and expressions - see this answer for more information.