如何在 c# 中使用 System.Diagnostics.Process.Start 运行 PowerShell 脚本?

How to run PowerShell script using System.Diagnostics.Process.Start in c#?

我正在写这个:

using System.Diagnostics;
Process.Start("C:\CodeProjects\C#\WindowsPowerShell\v1.0\powershell_ise.exe", "-File .\mp4_to_flac.ps1");

所有这一切都是在 Windows PowerShell ISE 中 打开 脚本。但我 也希望它 运行! 那么我还必须传入哪些其他参数才能执行?

Process.Start method Reference

你可以这样称呼它:

Process.Start(".\mp4_to_flac.ps1");

并确保所有 windows 设置都正确,因此双击文件即可执行。

你不想 运行 powershell_ise.exe 但 powershell.exe。在 DOS 命令提示符下,您可以在命令或脚本前加上@powershell,但对于一个进程,您将要使用类似

的东西

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

(这是我在 Win 8.1 上的),如果你使用的是不同的版本,你的应该在附近的某个地方。

chocolatey 家伙投入了这些开关

-NoProfile -ExecutionPolicy unrestricted

从命令提示符执行 powershell 时,您可能还必须这样做,具体取决于执行策略的设置方式。