views:

701

answers:

4

Guys and Gals, a really stupid question:

How do I run a PowerShell script?

  • I have a script named myscript.ps1
  • I have all the necessary frameworks installed
  • I set that execution policy thing
  • I have followed the instructions on this MSDN help page and am trying to run it like so: powershell.exe 'C:\my_path\yada_yada\run_import_script.ps1' (with or withot --noexit)

which returns exactly nothing, except that the file name is output. No error, no message, nothing. Oh, when I add -noexit, the same thing happens but I remain within Powershell, and have to exit manually.

The ps1 file is supposed to run a program, and return the error level dependant on that program's output. But I'm quite sure I'm not even getting there yet.

What am I doing wrong?

+2  A: 
  1. Launch PowerShell
  2. Navigate to the directory where the script lives

    PS> cd C:\my_path\yada_yada\ (enter)
    
  3. Execute the script:

    PS> .\run_import_script.ps1 (enter)
    

What am I missing??

Or: you can run the PowerShell script from cmd.exe like this:

powershell -noexit "& "C:\my_path\yada_yada\run_import_script.ps1" (enter)

according to this blog post here

Or you could even run your Powershell script from your C# app :-)

Asynchronously execute PowerShell scripts from your C# application

marc_s
This indeed works, but I need to do this from within a batch file. Obviously, my way of calling `powershell.exe` and then the script file is somehow screwed up. Do you have any idea how to modify it?
Pekka
Pekka
you're right - I posted the wrong command line - the one that doesn't work :-) The one that does work is in the blog post and I fixed my wrong post now :-)
marc_s
+3  A: 

If you are on PowerShell 2.0 use PowerShell.exe's -File parameter to invoke a script from another environent like cmd.exe e.g.:

Powershell.exe -File C:\my_path\yada_yada\run_import_script.ps1
Keith Hill
Is there a way to add parameters to such an invocation?
Alexander Groß
You should just be able to trail the args after the script file path. From the PowerShell.exe usage - [-File <filePath> <args>]
Keith Hill
+1  A: 

I've had the same problem, and tried and tried... finally i used:

powershell.exe -noexit "& 'c:\Data\ScheduledScripts\ShutdownVM.ps1'"

And put this line in a batch-file, and this works.

Dennis
A: 

If you only have powershell 1.0, this seems to do the trick well enough.

   powershell -command - < c:\mypath\myscript.ps1

It pipes the script file to the powershell command line.

AndyM