How-to: Run a PowerShell script

There are several ways to run a PowerShell script.

Before running any scripts on a new PowerShell installation, you must first set an appropriate Execution Policy

PS C:\> Set-ExecutionPolicy RemoteSigned

If the script has been downloaded from the internet and saved as a file then you may also need to right click on the script, select properties, then unblock. If you just copy and paste the text of the script, this is not needed.

A PowerShell script is the equivalent of a Windows CMD or MS-DOS batch file, the file should be saved as plain ASCII text with a .ps1 extension, e.g. MyScript.ps1

Run, Call or Invoke a PowerShell script

Run a PowerShell script located in the current directory from the PowerShell console:
PS C:\> ./Myscript.ps1
you can also use a backslash:
PS C:\> .\Myscript.ps1
and pass parameters:
PS C:\> ./Myscript.ps1 -length 123

The reason that dot-slash is required is that the current directory is not likely to be in the system PATH, so for security PowerShell will insist that you are specific about the location of the file to be run, either referencing the current directory '.' or by givng a full pathname:

PS C:\> C:\scripts\Myscript.ps1

If the script you want to run uses a pathname with spaces, this gets more complex, if you just add double quotes, that will appear to PowerShell as a "string" to be echoed, not a command to be executed:

PS C:\> "C:\my fave scripts\Myscript.ps1"
"C:\my fave scripts\Myscript.ps1"

To instruct PowerShell to actually run this script/executable, use the CALL operator:

PS C:\> & "C:\my fave scripts\Myscript.ps1"

This is the most common way to run PowerShell scripts.

Call one PowerShell script from another script saved in the same directory:

#Requires -Version 3.0
& "$PSScriptRoot\set-consolesize.ps1" -height 25 -width 90

Run a PowerShell Script from the GUI or with a shortcut

This can be done by running PowerShell.exe with parameters to launch the desired script.

Run As Administrator (Elevated)

See the PowerShell elevation page for ways of running a script or a PowerShell session "As admin"

Dot Sourcing

Dot sourcing is very similar to the CALL operator, but when you dot source a script, all variables and functions defined in the script will persist even when the script ends.

Run a script by dot-sourcing it:

PS C:\> . "C:\Batch\My first Script.ps1"

Dot-sourcing a script in the current directory:

PS C:\> . .\Myscript.ps1"

Run a CMD batch file

Run a CMD batch file from PowerShell:

PS C:\> ./demo.cmd

You can also use a backslash:

PS C:\> .\demo.cmd

and pass parameters:

PS C:\> ./demo.cmd '123'

The reason that dot-slash prefix is required is that the current directory is not likely to be in the system PATH, so for security PowerShell will insist that you are specific about the location of the file to be run, either referencing the current directory '.' or by givng a full pathname:

PS C:\> C:\scripts\demo.cmd

If the script you want to run uses a pathname with spaces, this gets more complex, if you just add double quotes, that will appear to PowerShell as a "string" to be echoed, not a command to be executed:

PS C:\> "C:\my fave scripts\demo.cmd"
"C:\my fave scripts\demo.cmd"

To instruct PowerShell to actually run this script/executable, use the CALL operator:

PS C:\> & "C:\my fave scripts\demo.cmd"

Early versions of PowerShell would only run internal CMD commands if the batch file was run by explicitly calling the CMD.exe shell and passing the batch file name.

Run a single CMD internal command

This will run the CMD.exe version of DIR rather than the PowerShell DIR alias for Get-ChildItem:

PS C:\> CMD.exe /C dir

Run a script on one or more remote computers

Use the -FilePath parameter of Invoke-Command:

Invoke-Command -ComputerName Server64,Server65 -FilePath C:\Scripts\Get-ServiceLog.ps1

When you invoke a script using the syntax above, variables and functions defined in the script will disappear when the script ends. Unless they are explicitly defined as globals: Function SCOPE:GLOBAL or Filter SCOPE:GLOBAL or Set-Variable -scope "Global"

Run a VBScript file

Run a vb script from PowerShell:
PS C:\> cscript c:\batch\demo.vbs

or for a path with spaces:
PS C:\> & "cscript c:\my fave scripts\demo.vbs"

The System Path

If you run a script (or even just enter a command) without specifying the fully qualified path name, PowerShell will search for it as follows:

  1. Currently defined aliases
  2. Currently defined functions
  3. Commands located in the system path.

#Yeah, I’m gonna run to you, cause when the feelin’s right I’m gonna stay all night, I’m gonna run to you# ~ Bryan Adams

Related PowerShell Cmdlets

#requires - Prevent a script from running without a required element.
Basic PowerShell script Template - HowTo.
Invoke-Command - Run commands on local and remote computers.
Invoke-Expression - Run a PowerShell expression.
Invoke-Item - Invoke an executable or open a file (START).
The call operator (&) - Execute a command, script or function.
Set-Variable - Set a variable and its value.
Functions - Write a named block of code.
PowerShell.exe - Launch PowerShell.
CMD Shell: Run a PowerShell script from the CMD shell.


 
Copyright © 1999-2024 SS64.com
Some rights reserved