3

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

Awesome_girl
  • 484
  • 3
  • 9
  • 30

2 Answers2

3

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.

Miniver Cheevy
  • 1,667
  • 2
  • 14
  • 20
  • so simple! thanks, i just took out "_ise" from my code. It executes instead of opening powershell ISE. Great. :) – Awesome_girl Mar 22 '15 at 21:06
  • any idea why running PS script using process doesn't load all the modules ? my script contains `Import-Module ServerManager` which runs fine when manually executed, but from process it fails `Import-Module : The specified module \ServerManager was not loaded no valid module file was found` – Reddysekhar Gaduputi Feb 08 '19 at 06:32
-1

You could call it like so:

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

and make sure all your windows settings are correct, so a double click on the file will execute it.

DrKoch
  • 9,556
  • 2
  • 34
  • 43
  • 1
    Double-clicking on .ps1 will open it for edit with the correct default settings. This is a security measure. – Andrew Savinykh Mar 22 '15 at 20:14
  • On my machine I changed the setting so double click will execute the .ps1. With this setting the approach in my answer will work. – DrKoch Mar 22 '15 at 20:16
  • 1
    I personally do not consider that helpful, because not everyone is willing to change default global (secure) settings to non-default and less secure so that a single program could work. – Andrew Savinykh Mar 22 '15 at 20:18
  • @DrKoch, how did you change your setting to execute .ps1 with powershell? – Awesome_girl Mar 22 '15 at 21:03