Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm writing this:

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

All this does is open up the script in Windows PowerShell ISE. But I also want it to RUN! So what other arguments do I have to pass in so that it executes?

Process.Start method Reference

like image 931
Awesome_girl Avatar asked Mar 27 '26 06:03

Awesome_girl


1 Answers

You don't want to run powershell_ise.exe but powershell.exe. From a dos command prompt you can just prefix your command or script with @powershell, but for a process you're going to want to use something like

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

(that's mine on Win 8.1), yours should be somewhere near there if you're on a different version.

The chocolatey guys throw in these switches

-NoProfile -ExecutionPolicy unrestricted

when executing powershell from the command prompt, you might have to do that also depending on how your execution policy is set.

like image 78
Miniver Cheevy Avatar answered Mar 28 '26 19:03

Miniver Cheevy