You can run a PowerShell script by opening the terminal and entering the full path to the file with the .ps1 extension preceded by the . operator, but you must first ensure that the local execution policy is set to allow unsigned scripts (like RemoteSigned) or bypass it temporarily for the command to execute.

What You'll Need

  • A computer with Windows installed (PowerShell is built into the operating system).
  • A text editor or an integrated development environment (IDE) like Visual Studio Code, Notepad, or VS Code to create or edit the script file.
  • The PowerShell script file itself (ending with .ps1) stored on your local hard drive or on a network share.
  • Administrative privileges (Run as Administrator) for many system-level scripts.

Step-by-Step

  1. Locate the script file. Open File Explorer and navigate to the folder containing your PowerShell script. Note the full path to the file. For example, if the file is named update_system.ps1 and is on your Desktop, the full path is C:\Users\YourUsername\Desktop\update_system.ps1.
  2. Open PowerShell. Press the Windows key, type "PowerShell" or "Windows PowerShell", and press Enter. To run scripts that modify system settings or install software, it is highly recommended to right-click the application in the Start menu and select "Run as administrator". This avoids permission errors later in the process.
  3. Check the Execution Policy (Crucial First Step). PowerShell, by default, blocks scripts for security reasons to prevent the execution of malicious code. Before running your script, type the following command and press Enter:
    Get-ExecutionPolicy
    If the output is Restricted, Default, or Undefined, the script will fail immediately. You must change this policy to allow the script to run. To allow scripts signed locally but block remote ones, run:
    Set-ExecutionPolicy RemoteSigned
    Type Y and press Enter to confirm. If you only need to run this one script for testing and do not want to change the system-wide setting, you can bypass the policy check for the current session using the -ExecutionPolicy Bypass flag (see Tip #2 below).
  4. Enter the command to run the script. Return to the PowerShell window. You will use the . operator followed by the filename. The dot-backslash combination tells PowerShell to look in the current directory. Type the following command and press Enter:
    .
    update_system.ps1
    If you are not in the same folder as the script, you must provide the full path instead of just the filename. For example:
    "C:\Users\YourUsername\Desktop\update_system.ps1"
    It is good practice to wrap the path in double quotes if it contains spaces.
  5. Review the output. The script will now begin executing line by line. You will see text appear in the terminal window. This text is the output of your script, including any print statements, status updates, or errors. Watch for any red text, which indicates an error.
  6. Verify the results. Once the script finishes, check the application or system setting that the script was designed to change. For example, if the script was meant to create a new user, check your user list to confirm the user was created. If the script failed, the text output will usually tell you why.

Common Mistakes

1. Forgetting the . prefix. A very common error is typing just the filename, like update_system.ps1. Without the . , PowerShell interprets this as a command name (like a built-in tool or a new command you are defining), not a file path. This will result in an error stating that the term is not recognized.

2. Ignoring Execution Policies. Many users run the command and receive the error "You cannot run a PowerShell script because scripts are disabled on this system." This is almost always because the Execution Policy is set to Restricted. Skipping this step is the #1 reason scripts fail to run on new Windows installations or corporate machines.

3. Not running as Administrator. If your script is trying to modify system settings, install drivers, or write to protected folders (like C:\Windows), standard user privileges will block the actions. The script may appear to run but will silently fail to complete its tasks.

Tips

  • Use -ExecutionPolicy Bypass for testing. If you do not want to change the system-wide Execution Policy or are unsure what it is set to, you can override it specifically for the command you are running. Add this flag to the end of your command line:
    .
    update_system.ps1 -ExecutionPolicy Bypass
    This allows the script to run without changing the global security setting.
  • Use the cd command to change directories. Instead of typing the full path every time, you can navigate to the script's folder first. Type cd followed by the path to the folder (e.g., cd C:\Scripts) and press Enter. Once you are in that folder, you can run the script using just the filename.
  • Use a text editor with PowerShell syntax highlighting. While Notepad works, it does not highlight errors or understand PowerShell syntax. Using Visual Studio Code with the PowerShell extension provides IntelliSense, syntax checking, and error highlighting while you write the script.
  • Pass arguments to the script. Your script can be designed to accept input from the command line. For example, a script might ask for a computer name. You can pass that name directly when running the script:
    .
    create_backup.ps1 -ComputerName PC001
    The script can then access this variable using $args[0] or a specific parameter name if defined.

Troubleshooting

Issue: The term 'scriptname.ps1' is not recognized. Diagnosis: This usually means the . prefix was missing, or the script is located in a different directory than where PowerShell is currently running. PowerShell does not automatically search the entire hard drive for a file. Solution: Add the . prefix back to the command. If that does not work, use the Get-ChildItem (Get-Location) command to list the files in the current directory and confirm the filename is spelled correctly, including the .ps1 extension.

Issue: You cannot run a script because running scripts is disabled on this system. Diagnosis: The system's Execution Policy is blocking the script. This is a security feature. Solution: Run PowerShell as Administrator and execute the Set-ExecutionPolicy RemoteSigned command. Alternatively, use the -ExecutionPolicy Bypass flag in your script command to allow it to run for this session only.

Issue: Access is denied. Diagnosis: The script is trying to access a resource (like a file or registry key) that requires higher privileges than the current user has. Solution: Close the current PowerShell window and reopen it by right-clicking on the Start menu or the PowerShell icon and selecting "Run as administrator". Re-enter your command.

Issue: The script runs but does nothing. Diagnosis: This often happens if the script contains a syntax error on a line that is executed before the rest of the script, or if the script was saved with the wrong file extension (e.g., .txt instead of .ps1). Solution: Check the PowerShell window for red error text at the top. If there is no error shown, open the script file in a text editor and verify that the .ps1 extension is correct and that the code is valid PowerShell syntax.

Frequently Asked Questions

Can I run a PowerShell script from a URL?

No, you cannot directly run a script hosted on a website using the standard . filename.ps1 command. PowerShell treats remote content as potentially unsafe. To run a script from a URL, you would typically use the Invoke-WebRequest cmdlet to download the file to your computer first, and then execute the local file.

Why does my script fail with an 'Access Denied' error?

PowerShell scripts run in the security context of the user who launched the window. If the script is trying to create a system user or modify a registry key, standard user accounts do not have permission. You must launch PowerShell with "Run as Administrator" privileges to bypass this restriction.

How do I pass a password to a script securely?

You should never pass a password in plain text as a command-line argument because it will be visible in the system process list and can be seen by other users on the computer. The secure method is to use the Read-Host -AsSecureString cmdlet within the script. This prompts the user to type the password, and it is stored as an encrypted string that cannot be easily read.

Can I run a PowerShell script on a remote computer?

Yes, but you need to enable PowerShell Remoting on the target machine. The standard way to do this is by running the Enable-PSRemoting cmdlet as an administrator on the remote computer. Once enabled, you can run commands on the remote machine using the Invoke-Command cmdlet.

What is the difference between PowerShell and Command Prompt?

PowerShell is newer and more powerful. While Command Prompt (cmd.exe) is limited to basic file operations and simple batch commands, PowerShell is a full scripting language that can automate complex tasks, manage the Windows registry, and interact with .NET objects. It is the recommended tool for system administration.