822

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 without --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 dependent on that program's output. But I'm quite sure I'm not even getting there yet.

What am I doing wrong?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Pekka
  • 418,526
  • 129
  • 929
  • 1,058
  • 6
    Start the `powershell` as you would have started `cmd`. Now you can execute the `myscript.ps1` script as any executable there in (in powershell window), i.e. `.\myscript.ps1` – parasrish Dec 19 '17 at 03:22

15 Answers15

833
  1. Launch Windows PowerShell, and wait a moment for the PS command prompt to appear
  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# application :-)

Asynchronously execute PowerShell scripts from your C# application

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
  • 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 Jan 09 '10 at 22:26
  • 23
    Your blog post link did it. I have to use `powershell -noexit "& "C:\yada_yada\run_import_script.ps1"` (notice the *three* double quotes) I don't really understand why, but at this point, I don't really care :) Thanks a lot! – Pekka Jan 09 '10 at 22:32
  • 17
    What exactly does the `"&` do? – BlueRaja - Danny Pflughoeft Jan 25 '12 at 02:00
  • 18
    According to http://technet.microsoft.com/en-us/library/ee176949.aspx, the '&' is for "If you actually want to execute that string value (that is, if you want to run the script whose path is enclosed in double quotes) you need to preface the path with the Call operator (the ampersand)." – Doug Dawson May 29 '12 at 14:32
  • 7
    Just use the command `powershell c:\mypath\yadayada\myimportantscript.ps1` if your path and file name have no spaces in it but if you put quotes around it powershell will try and interpret the parameter as a string of powershell commands. – BeowulfNode42 Feb 09 '14 at 20:55
  • 3
    @marc_s: Or just right-click the file and choose "Execute with Powershell". At least that works under Windows 7. – juergen d Aug 04 '14 at 12:04
  • 1
    Downvoted. Doesn't answer how to execute from a batch file, doesn't clearly answer how to execute it in a single command. Generally insufficient to be a good answer. – Thomas W Sep 17 '14 at 04:56
  • 37
    none of these worked for me until I bypassed powershell's default executionpolicy for my freshly-created unsigned script. Among other things this required restarting powershell As Administrator. Totally agree with @LukePuplett - it's brilliant to make the simplest use-case take 20 minutes of googling and futzing around. And the error messages! Apparently these guys worked at IBM... in the 70's. – Spike0xff Mar 01 '16 at 21:49
  • 16
    As stated in a comment below, the execution policy has to be bypassed. One of the items that is not discussed here is how to run a powershell script from a command line passing in a parameter. To do this: type `powershell -executionpolicy bypass ".\myscript.ps1 yadayadayada"` – Jim Lahman Mar 14 '16 at 15:48
  • ?? 500+ people have never heard of love bug - iloveyou.vbs – Sum1sAdmin Apr 11 '18 at 15:02
  • Note that the path ('.\') has to be specified or it won't work. I tried without the path and got 'The term 'scriptname.ps1' is not recognized as the name of a' ... – Jason Cheng Feb 05 '21 at 20:47
265

If you are on PowerShell 2.0, use PowerShell.exe's -File parameter to invoke a script from another environment, like cmd.exe. For example:

Powershell.exe -File C:\my_path\yada_yada\run_import_script.ps1
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Keith Hill
  • 173,872
  • 36
  • 316
  • 347
207

If you want to run a script without modifying the default script execution policy, you can use the bypass switch when launching Windows PowerShell.

powershell [-noexit] -executionpolicy bypass -File <Filename>
Chingiz Musayev
  • 2,594
  • 1
  • 12
  • 6
92

Type:

powershell -executionpolicy bypass -File .\Test.ps1

NOTE: Here Test.ps1 is the PowerShell script.

igr
  • 8,815
  • 10
  • 56
  • 98
Sudheesh
  • 921
  • 6
  • 2
  • This should be executed in a powershell as `powershell -executionpolicy bypass -File .\Test.ps1` assuming you current working directory contains Test.ps1 – Yeow_Meng Dec 03 '15 at 03:57
32

I've had the same problem, and I 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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Dennis
  • 1,689
  • 1
  • 18
  • 41
22

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
AndyM
  • 3,374
  • 5
  • 36
  • 44
21

Pretty easy. Right click the .ps1 file in Windows and in the shell menu click on Run with PowerShell.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
electronictonic
  • 241
  • 2
  • 2
  • This works to quickly run a script without having to enable the execution of scripts with execution policy. Thanks! – b01 May 21 '17 at 00:02
12

Using cmd (BAT) file:

@echo off
color 1F
echo.

C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "PrepareEnvironment.ps1"

:EOF
echo Waiting seconds
timeout /t 10 /nobreak > NUL

If you need run as administrator:

  1. Make a shortcut pointed to the command prompt (I named it Administrative Command Prompt)
  2. Open the shortcut's properties and go to the Compatibility tab
  3. Under the Privilege Level section, make sure the checkbox next to "Run this program as an administrator" is checked
Kiquenet
  • 13,271
  • 31
  • 133
  • 232
12

If your script is named with the .ps1 extension and you're in a PowerShell window, you just run ./myscript.ps1 (assuming the file is in your working directory).

This is true for me anyway on Windows 10 with PowerShell version 5.1 anyway, and I don't think I've done anything to make it possible.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
rainabba
  • 3,067
  • 27
  • 29
  • 2
    How does this answer the question? – Peter Mortensen Mar 02 '17 at 10:29
  • 4
    it absolutely answers the question: how do I run a powershell script? answer: startup powershell console, then execute the script. easy. simple. Works on Linux also. – Thufir Feb 17 '18 at 18:31
  • 4
    This absolutely answers the question, and was exactly what I was looking for. as `myscript.ps1` did not work, threw an error, but with `./` it's executing. – Jeff Mar 31 '18 at 22:16
11

An easy way is to use PowerShell ISE, open script, run and invoke your script, function...

Enter image description here

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
9

In case you want to run a PowerShell script with Windows Task Scheduler, please follow the steps below:

  1. Create a task

  2. Set Program/Script to Powershell.exe

  3. Set Arguments to -File "C:\xxx.ps1"

It's from another answer, How do I execute a PowerShell script automatically using Windows task scheduler?.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Eugene
  • 8,507
  • 3
  • 35
  • 55
8
  • Give the path of the script, that is, path setting by cmd:

    $> . c:\program file\prog.ps1

  • Run the entry point function of PowerShell:

    For example, $> add or entry_func or main

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
pkm
  • 2,473
  • 1
  • 22
  • 41
  • Running your commands from a cmd prompt: `>$ . c:\program file\prog.ps1 '.' is not recognized as an internal or external command, operable program or batch file.` and `>$ add or entry_func or main 'add' is not recognized as an internal or external command, operable program or batch file.` – Suncat2000 Dec 09 '19 at 21:15
8

I have a very simple answer which works:

  1. Open PowerShell in administrator mode
  2. Run: set-executionpolicy unrestricted
  3. Open a regular PowerShell window and run your script.

I found this solution following the link that was given as part of error message: About Execution Policies

Edit: Make sure to run set-ExecutionPolicy default once you're done, or you will be exposed to seurity risks (thanks Anonymous user).

solstinger
  • 395
  • 3
  • 12
  • Make sure to run `set-ExecutionPolicy default` once you're done, or you will be exposed to seurity risks. – Anonymous Mar 21 '21 at 23:21
2

You can run from cmd like this:

type "script_path" | powershell.exe -c -
programmer365
  • 12,641
  • 3
  • 7
  • 28
1

Use the -File parameter in front of the filename. The quotes make PowerShell think it is a string of commands.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Engineer
  • 684
  • 10
  • 23