You don't need cmd
here at all1. You can spawn a process without a shell just as well. Furthermore, it's usually a good idea to quote arguments to avoid surprises with argument parsing (cmd
for example has its own meaning for parentheses, which may well interfere here).
powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://site',$Env:Temp + 'file.txt');Invoke-Item $Env:Tempfile.txt"
I've also added quotes around the URL you want to download, since that wouldn't otherwise work, either. And since cmd
is no longer around, environment variables can be expanded by PowerShell as well, with a different syntax.
Start-Process
also is for starting processes and Invoke-Item
is closer to what you actually want, although I'm sure with ShellExecute
behavior, Start-Process
could launch Notepad with a text file as well if desired.
1 If in doubt, it's always a good idea to reduce the number of parts, processes and different wrapped concepts needed. Same reason why you don't use Invoke-Expression
in PowerShell to run other programs: Unnecessary, and complicates everything just further by introducing another layer of parsing and interpretation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…