How to Run ps1 File from PowerShell

Running from PowerShell Command-Line

To run a PowerShell script(.ps1 file) from a PowerShell command line:

  • Launch the PowerShell as an Administrator and wait for the PS> prompt to appear.
  • Navigate to the directory having your PowerShell script file.
  • Type .\ followed by the ScriptFileName.ps1 and hit Enter to execute the script.

You must fulfil some prerequisites to execute the PowerShell script by following the above mentioned steps. First, you must be able to run the PowerShell as an Administrator. Second, you must be able to ByPass the PowerShell execution policy or set it to a permissive value.

Now, the point is, why do we have to set or ByPass the execution policy? It is because the PowerShell execution policy is set to Restricted due to its default behaviour, which means you can not run the .ps1 file. For the execution policy, we have a few options that are listed below:

  • Restricted – It is the default option, which does not allow running a PowerShell script.
  • Unrestricted – We can not run any script; it shows warnings for the downloaded scripts.
  • AllSigned – It allows running signed scripts from trusted publishers only.
  • ByPass – It lets us run all scripts without any warnings.
  • RemoteSigned – This option needs a digital signature for the downloaded scripts. We can execute scripts that are written locally and unblock the downloaded scripts to execute them without the signature.

You can check this for more details about these options. We can also run the .ps1 by Right Click on the script file and selecting Run with PowerShell.

Running from Windows Command-Line

Use powershell -noexit to run a PowerShell script from the Windows Command line (cmd.exe).

The above command was used to run the specified PowerShell script.

Let’s split it into chunks to understand each part of it.

First, we used the powershell to launch the PowerShell environment while the -noexit parameter was used to keep the PowerShell window open even after the script was finished executing. This way, we can see the output generated by the script file that just ran. Next, we used the call operator represented by the & sign to run the command, script block or expression.

This command is run from the Windows Run Dialog (opened with Windows key+ R) or from the command prompt as we did above. Note that the output will be visible in the PowerShell window if you run it from Windows Run Dialog. On the other hand, if you run it from Windows Command Prompt, you will see the output without opening PowerShell Window.

Using Invoke Expression Cmdlet

To run a ps1 file from PowerShell command line:

  • Launch the PowerShell as an Administrator and wait for the PS> prompt to appear.
  • Navigate to the location where your PowerShell script file lives.
  • Use the Invoke-Expression cmdlet to run the PowerShell script.

The Invoke-Expression cmdlet is used to run the expression or commands on our local computer. It runs or evaluates the given string as a command and returns the outcomes of the specified command or expression.

That’s all about how to run ps1 File From PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *