Skip to main content

How to Run a PowerShell Script File

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A PowerShell script is a text file of commands with a .ps1 extension, and you run it by typing its path, as in ./hello.ps1. This guide writes a two-line script, runs it, shows why its name alone isn't enough, and runs a script whose name has a space, with real output from PowerShell 7.6.10.

The about_Scripts page: "A script is a plain text file that contains one or more PowerShell commands. PowerShell scripts have a .ps1 file extension."

Syntax​

Syntax
./<script>.ps1
& '<path with spaces>.ps1'
PartWhat it means
./The current folder: . stands for the folder you're in, and / separates it from the file name
<script>.ps1The script's file name, with its .ps1 extension
&The call operator: runs the script whose path follows it
'<path with spaces>.ps1'A path that contains a space, in quotes

The about_Scripts page: "To run a script in the current directory, type the path to the current directory, or use a dot to represent the current directory, followed by a path backslash (.\)." This guide uses ./, with the forward slash that macOS paths use.

The Example​

The example runs a script named hello.ps1, which prints a greeting, from a folder named run-script-demo. The commands run in the PowerShell 7.6.10 console on a Mac.

Prerequisites​

  • PowerShell 7. This guide uses PowerShell 7.6.10: $PSVersionTable.PSVersion.ToString() printed 7.6.10.

Step 1: Write the Script​

Save these two lines in a file named hello.ps1, in the folder you'll run it from:

hello.ps1
$name = 'world'
"Hello, $name, from a script file"

macOS-style code window titled hello.ps1 with two lines: the first saves the text world in the variable $name, and the second is the greeting Hello, $name, from a script file in double quotes.

The first line saves the text world in the variable $name. The second is text in double quotes, so PowerShell puts the value of $name into it and prints the result. The about_Scripts page: "To write a script, open a new file in a text editor, type the commands, and save them in a file with a valid filename with the .ps1 file extension."

Step 2: Run the Script​

In PowerShell, from the folder that holds hello.ps1, type:

PowerShell
./hello.ps1
Output
Hello, world, from a script file

PowerShell ran the script's two lines in order and printed the greeting. If the console is in another folder, move to the script's folder first with Set-Location, followed by the folder's path.

Step 3: See Why ./ Is Needed​

PowerShell
hello.ps1
Output
hello.ps1: The term 'hello.ps1' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

[General Feedback]
The command "hello.ps1" was not found, but does exist in the current location.
PowerShell does not load commands from the current location by default (see 'Get-Help about_Command_Precedence').

If you trust this command, run the following command instead:
➤ ./hello.ps1

macOS-style terminal window titled PowerShell where typing hello.ps1 alone fails with The term hello.ps1 is not recognized, followed by General Feedback that the command exists in the current location and the suggestion to run ./hello.ps1 instead.

The file was right there, but PowerShell didn't run it from the current folder, and its feedback suggested ./hello.ps1 instead. The about_Scripts page: "As a security feature, PowerShell does not run scripts when you double-click the script icon in File Explorer or when you type the script name without a full path, even when the script is in the current directory."

Step 4: Run a Script Whose Name Has a Space​

A second script, my script.ps1, holds one line:

my script.ps1
'Hello from my script.ps1'

Run it with the call operator and its path in quotes:

PowerShell
& './my script.ps1'
Output
Hello from my script.ps1

The quotes kept the path together, and & ran the file it names. The about_Scopes page: "Files that have spaces in their path must be enclosed in quotes. If you try to execute the quoted path, PowerShell displays the contents of the quoted string instead of running the script. The call operator (&) allows you to execute the contents of the string containing the filename."

Common Mistakes​

MistakeWhat happensDo this instead
Running it from another folder: ./hello.ps1 after Set-Location .../hello.ps1: The term './hello.ps1' is not recognized as a name of a cmdlet, function, script file, or executable program.Go back to the script's folder, or type the script's full path
A space and no quotes: ./my script.ps1./my: The term './my' is not recognized as a name of a cmdlet, function, script file, or executable program.Quote the path and put & before it
Quotes but no &: './my script.ps1'It printed ./my script.ps1, the path as text, and didn't run the scriptPut & before the quoted path

For the first row, the about_Scripts page: "To run a script, type the full name and the full path to the script file."

FAQ​

Do I need to change the execution policy on a Mac?​

No. Get-ExecutionPolicy printed Unrestricted. The about_Execution_Policies page: "On non-Windows computers, the default execution policy is Unrestricted and can't be changed."

What about Windows?​

The about_Scripts page: "Before you can run a script on Windows, you need to change the default PowerShell execution policy." And: "The default execution policy, Restricted, prevents all scripts from running, including scripts that you write on the local computer." This guide ran on a Mac, so it doesn't show what Windows prints when it blocks a script.

Does .\hello.ps1 work on a Mac?​

Yes. .\hello.ps1, with the backslash that the Learn page shows, printed the same greeting on this Mac.

Conclusion​

./hello.ps1 ran the script and printed Hello, world, from a script file. Put ./ before the name of a script in the current folder, quote a path that has a space and put & before it, and on Windows check the execution policy first.

Additional Resources​