Software & OS

How to Run a PS1 File in Windows PowerShell

To run a PS1 file, open PowerShell in the folder that contains it and type .\script.ps1, replacing script with your file name. If Windows refuses, the cause is almost always the execution policy or a "downloaded from the internet" marker on the file, and both have one-line fixes below.

The quickest working sequence for a script you trust:

cd C:\Scripts
.\script.ps1

Here C:\Scripts is an example folder. Use your own.

How to run a PS1 file from a PowerShell window

A PS1 file is a plain text file of PowerShell commands. Microsoft's documentation says to run it by typing the full name and full path of the script, for example C:\Scripts\Get-ServiceLog.ps1. To run a script in the current directory, put .\ in front of the name. PowerShell does not run a script by bare name, even if the script sits in the folder you are in, so script.ps1 alone fails and .\script.ps1 works.

Any parameters go after the file name:

.\Get-ServiceLog.ps1 -ServiceName WinRM

That example comes from the Microsoft documentation. Your script defines its own parameter names, so check the top of the file for a param(...) block, or run Get-Help .\script.ps1.

A quick way to open PowerShell in the right folder: in File Explorer, click the address bar, type powershell and press Enter. The new window starts in that folder.

What to do when the path has spaces

A path with spaces must be in quotes, but a quoted path on its own is just a string. PowerShell prints it back instead of running it. Put the call operator & in front:

PS C:\Scripts> ".\script name with spaces.ps1"
.\script name with spaces.ps1
PS C:\Scripts> & ".\script name with spaces.ps1"
Hello World!

That is the example from Microsoft's about_Operators page: the first command only echoes the string, the second runs the script. The call operator does not split a string into a command and arguments, so keep the path in the quotes and put any parameters outside them: & "C:\My Scripts\backup.ps1" -Verbose.

Fix the execution policy error

The execution policy controls whether PowerShell loads scripts at all. Check what applies to you:

Get-ExecutionPolicy
Get-ExecutionPolicy -List

The second command lists every scope in precedence order. Microsoft's example output looks like this (example output, your values will differ):

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    RemoteSigned
 LocalMachine       AllSigned

Here the effective policy is RemoteSigned, because CurrentUser outranks LocalMachine. If every scope is Undefined, Microsoft says the effective policy is Restricted on Windows clients and RemoteSigned on Windows Server. The documentation is not consistent about which one is the shipped default on a client, so trust the output of Get-ExecutionPolicy on your machine rather than any article. Restricted permits typed commands but blocks all script files.

The policies you are likely to meet:

PolicyWhat it does with a script
RestrictedBlocks every script file.
AllSignedRuns only scripts signed by a trusted publisher, including ones you wrote yourself.
RemoteSignedRuns local scripts. Scripts downloaded from the internet must be signed, unless you unblock them.
UnrestrictedRuns unsigned scripts, with a warning for those not from the local intranet zone.
BypassBlocks nothing and shows no warnings.

Allow your own scripts for your user account only

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

This is the syntax from Microsoft's documentation. It is saved for your user, takes effect immediately, and needs no administrator rights. Without -Scope, the default is LocalMachine, which affects all users and needs an elevated prompt. If a Group Policy sets the policy (the MachinePolicy or UserPolicy rows), your change cannot override it, and the command reports that the scope is set by a Group Policy. On a work computer, ask your administrator.

Allow one script run without changing anything saved

Start a new session with a policy that lasts only for that session:

powershell.exe -ExecutionPolicy Bypass -File .\script.ps1

The -ExecutionPolicy parameter sets the policy for that session only and does not change the policy stored in the registry. -File must be the last parameter, because everything after it is read as the script path and its arguments. Use pwsh.exe instead of powershell.exe if you run PowerShell 7. Microsoft states plainly that the execution policy is not a security boundary: anyone can paste a script's contents into a prompt. It stops accidents, not a determined user, so do not treat Bypass as harmless for scripts you have not read.

Fix "not digitally signed" on a downloaded script

Browsers mark downloaded files as coming from the internet with an alternate data stream called Zone.Identifier. Under RemoteSigned, an unsigned script with that mark will not run. The Microsoft documentation shows this failure (example output):

PS C:\ps-test> C:\ps-test\Start-ActivityTracker.ps1
C:\ps-test\Start-ActivityTracker.ps1 : File C:\ps-test\Start-ActivityTracker.ps1 cannot
be loaded. The file C:\ps-test\Start-ActivityTracker.ps1 is not digitally signed. The script
will not execute on the system. For more information, see about_Execution_Policies.
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

To see which files in a folder carry the mark, then clear it on one file:

Get-Item * -Stream "Zone.Identifier" -ErrorAction SilentlyContinue
Unblock-File -Path .\script.ps1

You can get the same result in File Explorer: right-click the file, choose Properties, tick Unblock and click OK. Microsoft's own warning applies: review the file and its source before you unblock it. Unblocking tells Windows you vouch for the script.

Files fetched with curl.exe, Invoke-WebRequest or Invoke-RestMethod may not get the mark at all, according to the same documentation, so they can run without unblocking.

Run a PS1 file by right-clicking it

PowerShell does not run a script when you double-click it, which Microsoft describes as a security feature. The substitute is File Explorer's right-click menu: choose Run with PowerShell. Per the documentation, that starts a new session with this command:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file "%1"

The session closes when the script ends, so the console window appears only briefly, if at all. The feature is meant for scripts that take no parameters, print nothing you need to read and ask no questions. If your script prints results, run it from an open PowerShell window instead, or add Read-Host "Press Enter to close" as its last line so the window stays open. This menu item still follows your execution policy. Microsoft also notes that on Windows 11 the separate "Run with PowerShell 7" entry does not appear, because of a change in the context menu.

Run a PS1 file from Command Prompt or a batch file

From cmd.exe, start PowerShell and hand it the file:

powershell.exe -NoProfile -File "C:\Scripts\script.ps1"

-NoProfile skips your PowerShell profile so the run does not depend on your personal setup. Add -NoExit to keep the window open after the script finishes. Inside a batch file, refer to the script's folder with %~dp0, as in powershell.exe -File "%~dp0script.ps1". A relative .\script.ps1 there can fail with a "can't find the path" error, because it resolves against the current directory, which may not be the batch file's folder. If you want a general introduction to the other scripting format Windows ships, see What is a BAT file.

The process exit code is 0 unless the script calls exit with another number, or hits a terminating error, in which case it is 1. That matters when a scheduler or batch file checks whether the run succeeded.

Dot sourcing: run a script so its functions stay available

A script normally runs in its own scope, and its functions and variables disappear when it ends. To keep them in your current session, put a dot and a space before the path:

. .\UtilityFunctions.ps1

That is how you load a file of helper functions. For everyday scripts that just do a job, do not dot source.

Run a PS1 file on macOS or Linux

PowerShell 7 runs on all three platforms. On macOS and Linux, execution policy is not enforced: Microsoft says Get-ExecutionPolicy returns Unrestricted there and the behavior matches Bypass. From a terminal, run pwsh -File ./script.ps1. Scripts written for Windows may still fail if they call Windows-only commands. For more of the commands inside such scripts, see PowerShell basic commands.

If the script still will not run

  • The term is not recognized. You typed the name without .\ or a full path. Add .\.
  • Cannot find path. You are in a different folder. Run Get-Location to see where you are and Get-ChildItem *.ps1 to list the scripts there.
  • Policy change succeeded but nothing changed. A higher-precedence scope overrides it. Run Get-ExecutionPolicy -List and look at Process, CurrentUser and the two Group Policy rows.
  • Works in one window, fails in another. Windows PowerShell 5.1 (powershell.exe) and PowerShell 7 (pwsh.exe) keep separate configuration files, so their policies can differ. Run Get-ExecutionPolicy in the window that fails.
  • The script comes from a UNC path. Microsoft notes that a script identified by a network path might not be permitted under RemoteSigned on systems that do not distinguish UNC paths from internet paths. Copy it to a local folder and run it there.

Frequently asked questions

Why does double-clicking a .ps1 file not run it?
Microsoft documents this as a deliberate security feature: PowerShell does not run scripts when you double-click the script icon in File Explorer. Right-click the file and choose Run with PowerShell, or open a PowerShell window and type the path to the script.
What does Unblock-File do?
Unblock-File removes the Zone.Identifier alternate data stream that Windows attaches to files downloaded from the internet. With that marker gone, a downloaded unsigned script can run under the RemoteSigned policy. It performs the same operation as the Unblock button in the file's Properties dialog. Read the script before you unblock it.
Do I need administrator rights to run a script?
Not to run a script. You need an elevated prompt only to change the execution policy for the LocalMachine scope. Changing it for CurrentUser or for a single session does not need administrator rights, and neither does running the script itself unless the script touches protected resources.
Can I run a .ps1 file on macOS or Linux?
Yes, with PowerShell 7 (pwsh) installed. Execution policy is not enforced there, so you can run it with pwsh -File ./script.ps1 from a terminal. Windows PowerShell 5.1 and the powershell.exe command exist only on Windows.

Sources