[windows] How to run a PowerShell script

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?

This question is related to windows powershell scripting

The answer is


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

Enter image description here


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


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

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


Type:

powershell -executionpolicy bypass -File .\Test.ps1

NOTE: Here Test.ps1 is the PowerShell script.


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.


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

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


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.


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


You can run from cmd like this:

type "script_path" | powershell.exe -c -

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>

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.


  • 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


Examples related to windows

"Permission Denied" trying to run Python on Windows 10 A fatal error occurred while creating a TLS client credential. The internal error state is 10013 How to install OpenJDK 11 on Windows? I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? git clone: Authentication failed for <URL> How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" XCOPY: Overwrite all without prompt in BATCH Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory how to open Jupyter notebook in chrome on windows Tensorflow import error: No module named 'tensorflow'

Examples related to powershell

Why powershell does not run Angular commands? How do I install the Nuget provider for PowerShell on a unconnected machine so I can install a nuget package from the PS command line? How to print environment variables to the console in PowerShell? Check if a string is not NULL or EMPTY The term 'ng' is not recognized as the name of a cmdlet VSCode Change Default Terminal 'Connect-MsolService' is not recognized as the name of a cmdlet Powershell Invoke-WebRequest Fails with SSL/TLS Secure Channel Install-Module : The term 'Install-Module' is not recognized as the name of a cmdlet Change directory in PowerShell

Examples related to scripting

What does `set -x` do? Creating an array from a text file in Bash Windows batch - concatenate multiple text files into one Raise error in a Bash script How do I assign a null value to a variable in PowerShell? Difference between ${} and $() in Bash Using a batch to copy from network drive to C: or D: drive Check if a string matches a regex in Bash script How to run a script at a certain time on Linux? How to make an "alias" for a long path?