1042

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?

2
  • 9
    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
    Commented Dec 19, 2017 at 3:22
  • The MSDN/Technet URL now redirects to a page saying "The Windows PowerShell 1.0 Owner’s Manual has been retired. For the most up-to-date Windows PowerShell content, go to Using Windows PowerShell." I'll try to replace it with a valid URL if I have time this afternoon.
    – AJM
    Commented Mar 13 at 16:17

17 Answers 17

1054

Prerequisites:

  • You need to be able to run PowerShell as an administrator
  • You need to set your PowerShell execution policy to a permissive value or be able to bypass it

Steps:

  1. Launch Windows PowerShell as an Administrator, and wait for the PS> prompt to appear

  2. Navigate within PowerShell 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)
    

Or: you can run the PowerShell script from the Command Prompt (cmd.exe) like this:

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

according to Invoking a PowerShell script from cmd.exe (or Start | Run) by Kirk Munro.

Or you could even run your PowerShell script asynchronously from your C# application.

5
  • 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
    Commented Jan 9, 2010 at 22:26
  • 27
    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
    Commented Jan 9, 2010 at 22:32
  • 23
    What exactly does the "& do? Commented Jan 25, 2012 at 2:00
  • 22
    According to 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)." Commented May 29, 2012 at 14:32
  • 9
    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. Commented Feb 9, 2014 at 20:55
324

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
5
  • 6
    Is there a way to add parameters to such an invocation? Commented Feb 1, 2010 at 23:25
  • 13
    You should just be able to trail the args after the script file path. From the PowerShell.exe usage - [-File <filePath> <args>]
    – Keith Hill
    Commented Feb 2, 2010 at 1:32
  • "cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" fo r more details." Commented Oct 21, 2016 at 14:26
  • 10
    If you haven't enabled PowerShell script execution on your system add the parameter -ExecutionPolicy Bypass
    – Keith Hill
    Commented Oct 21, 2016 at 18:36
  • 5
    FYI this also works for PowerShell 1.0 in my experience (Windows 2012 Server)
    – knocte
    Commented Nov 8, 2016 at 7:55
239

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>
1
  • 14
    Also, include the -nologo option to get rid of the startup banner
    – swdev
    Commented Jun 1, 2016 at 6:28
127

In a command prompt type:

powershell -executionpolicy bypass -File .\Test.ps1

NOTE: Here Test.ps1 is the PowerShell script.

1
  • This should be executed in a powershell as powershell -executionpolicy bypass -File .\Test.ps1 assuming you current working directory contains Test.ps1
    – Yeow_Meng
    Commented Dec 3, 2015 at 3:57
40

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.

27

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.

1
  • 3
    Useful trick when security policies don't allow script execution. Commented Aug 21, 2016 at 14:46
26
  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 the error message: About Execution Policies

Make sure to run set-ExecutionPolicy default once you're done, or you will be exposed to security risks.

0
25

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

1
  • This works to quickly run a script without having to enable the execution of scripts with execution policy. Thanks!
    – b01
    Commented May 21, 2017 at 0:02
15

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.

4
  • 2
    How does this answer the question? Commented Mar 2, 2017 at 10:29
  • 6
    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
    Commented Feb 17, 2018 at 18:31
  • 6
    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
    Commented Mar 31, 2018 at 22:16
  • But what if script has no suffix?
    – 71GA
    Commented Nov 29, 2023 at 10:14
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
12

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

Enter image description here

0
12

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?.

9
  • 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

1
  • 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
    Commented Dec 9, 2019 at 21:15
5

I've just found the method what Microsoft do when we right click on a ps1 script and click on "Run with PowerShell" :

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'C:\Users\USERNAME\Desktop\MYSCRIPT.ps1'"
4

You can run from cmd like this:

type "script_path" | powershell.exe -c -
3

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

0
1

With the appropriate execution policy, you should just be able to call the file directly and Windows will associate it with PowerShell

C:\my_path\yada_yada\run_import_script.ps1

That does not do so well with arguments. The real answer to your question is that you are missing the & to say "execute this"

powershell.exe "& 'C:\my_path\yada_yada\run_import_script.ps1'"

For @ivan_pozdeev

To Run Any Windows Script

Batch

  • To run from File Explorer: double-click test.bat
  • To run from cmd.exe or *.bat or *.cmd:
.\test.bat
  • To run from PowerShell:
.\test.bat
  • Additional Registry edits
  • Many other ways

PowerShell

  • To run from File Explorer: right-click for context menu, then click "Run with PowerShell"
  • To run from cmd.exe or *.bat or *.cmd:
powershell.exe .\test.ps1
powershell.exe -File .\test.ps1
powershell.exe -C "& '.\test.ps1'"
  • To run from PowerShell:
.\test.ps1
  • Additional Registry edits
  • Many other ways
3
  • Windows does NOT associate ps1's Open command with running with Powershell. Instead, it opens the script for editing. Commented Jul 9 at 7:54
  • No, it does not. It will run from any command prompt. It does have a right-click "Run with PowerShell". It will run from a script with `PowerShell.exe "& 'C:\...'".
    – carrvo
    Commented Jul 13 at 4:13
  • Testing: from cmd prompt: echo ^&echo foo >test.ps1 test.ps1 => the file is opened in Notepad. Double-clicking it in Explorer produces the same result. "File type association" (without further specification) means the default ("open") shell command. "Calling the file directly" as in the 1st example only works from powershell prompt -- and only if the path doesn't have spaces. Commented Jul 13 at 4:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.