Pekka
Pekka

Reputation: 449783

How to run a PowerShell script

How do I run a PowerShell script?

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?

Upvotes: 1079

Views: 3376195

Answers (17)

carrvo
carrvo

Reputation: 648

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

Upvotes: 4

Sudheesh
Sudheesh

Reputation: 1319

In a command prompt type:

powershell -executionpolicy bypass -File .\Test.ps1

NOTE: Here Test.ps1 is the PowerShell script.

Upvotes: 131

JovanToroman
JovanToroman

Reputation: 705

  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.

Upvotes: 27

marc_s
marc_s

Reputation: 755297

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.

Upvotes: 1089

Roukmoute
Roukmoute

Reputation: 789

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'"

Upvotes: 5

wasif
wasif

Reputation: 15498

You can run from cmd like this:

type "script_path" | powershell.exe -c -

Upvotes: 4

Keith Hill
Keith Hill

Reputation: 202012

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

Upvotes: 337

Engineer
Engineer

Reputation: 914

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

Upvotes: 3

Eugene
Eugene

Reputation: 11085

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

Upvotes: 13

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

Enter image description here

Upvotes: 12

rainabba
rainabba

Reputation: 4277

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.

Upvotes: 16

electronictonic
electronictonic

Reputation: 291

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

Upvotes: 26

pkm
pkm

Reputation: 2783

  • 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

Upvotes: 9

AndyM
AndyM

Reputation: 3794

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.

Upvotes: 29

Dennis
Dennis

Reputation: 1858

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.

Upvotes: 42

Kiquenet
Kiquenet

Reputation: 15036

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

Upvotes: 12

Chingiz Musayev
Chingiz Musayev

Reputation: 2982

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>

Upvotes: 244

Related Questions