First, unless you specifically need to run the batch file in a new window, do not use Start-Process
- use direct invocation instead, which is implicitly synchronous and allows you to capture or redirect output:
# Invoke the batch file synchronously (wait for it to exit)
# and capture its (standard) output in variable $A
# To print the batch file's output to the console instead, just use:
# .\refresh.bat
$A = .\refresh.bat
See this answer for more information.
Also note Start-Process
never allows you to capture the invoked program's output directly (you can only redirect it to files with -RedirectStandardOutput
and -RedirectStandardOutput
); your specific command captures nothing[1] in $A
; adding -PassThru
does return something, but not the program's output, but a process-information object (System.Diagnostics.Process
).
If you do need to run the batch file in a new window and want to keep that window open:
Start-Process -Wait -FilePath cmd -ArgumentList '/k .\refresh.bat'
Relying on positional parameter binding, the above can be simplified to:
Start-Process -Wait cmd '/k .\refresh.bat'
[1] Strictly speaking, $A
is assigned the [System.Management.Automation.Internal.AutomationNull]::Value
singleton, which in most contexts behaves like $null
.