sodcalculator

10 Tech How-To

How to Create and Run PowerShell Scripts in Windows

Guide 10-30801 · Updated · Step by step

This guide walks you through creating and running PowerShell scripts on Windows, from enabling script execution to writing and executing your first .ps1 file. You’ll learn the essential commands, common pitfalls, and how to troubleshoot issues, so you can automate tasks efficiently.

What You’ll Need

  • A Windows PC with PowerShell (preinstalled on Windows 10/11, or Windows PowerShell 5.1).
  • Permission to run scripts – you may need administrator rights to change execution policy.
  • A text editor – Notepad works, but VS Code or the PowerShell ISE are better for editing.

Step-by-Step

  1. Open PowerShell – Press Win, type PowerShell, and select Windows PowerShell or PowerShell 7 (if installed). For scripts that need admin rights, right-click and choose Run as administrator.
  2. Check the execution policy – Run Get-ExecutionPolicy. If it returns Restricted, you cannot run scripts yet. To change it, run Set-ExecutionPolicy RemoteSigned (allows local scripts and signed remote scripts). You may need to confirm with Y. This change is safe for most users.
  3. Create a new script file – In your text editor, write your PowerShell commands. For example, a simple script that prints the current date and time:
    Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    Save the file with a .ps1 extension, e.g., myscript.ps1. Use UTF-8 with BOM encoding if you have non-ASCII characters.
  4. Navigate to the script’s folder – In PowerShell, use cd C:\path\to\folder to go to where you saved the script.
  5. Run the script – Type .\myscript.ps1 and press Enter. The .\ tells PowerShell to look in the current directory. If you get an error about execution policy, revisit step 2.
  6. Run with parameters (if any) – If your script accepts parameters, pass them after the filename, e.g., .\myscript.ps1 -Name "John".
  7. Make the script executable from anywhere (optional) – Add the script’s folder to your PATH environment variable, or create a shortcut that calls PowerShell with the script path.

Common Mistakes

  • Forgetting the .\ prefix – Running myscript.ps1 without .\ may not work because PowerShell does not search the current directory by default.
  • Execution policy blocks script – The most common error is “running scripts is disabled on this system”. Fix by setting RemoteSigned.
  • Wrong file extension – Saving as .txt or .ps1.txt will not run as a script.
  • Path issues – If your script uses relative paths, they are relative to the current working directory, not the script’s location. Use $PSScriptRoot to reference the script’s folder.

Tips

  • Use the PowerShell ISE or VS Code – They provide syntax highlighting, debugging, and easy running (F5 in ISE).
  • Comment your code – Use # for comments to explain what each part does.
  • Test with small scripts first – Start with simple commands before complex logic.
  • Use Write-Host or Write-Output to display progress.
  • Set execution policy for the current user only – Use Set-ExecutionPolicy -Scope CurrentUser RemoteSigned to avoid system-wide changes.

Troubleshooting

If your script doesn’t run, check:

  • Error: “File cannot be loaded because running scripts is disabled” – Run Set-ExecutionPolicy RemoteSigned -Scope CurrentUser and try again.
  • Error: “The term ‘script.ps1’ is not recognized” – Ensure you are in the correct directory using cd, and use .\ before the filename.
  • Script runs but nothing happens – Add Write-Host statements to see where it’s failing. Check for syntax errors with Get-Content or by opening in ISE.
  • Permission denied – If you need admin rights, right-click PowerShell and run as administrator.
  • Path with spaces – Enclose the path in quotes, e.g., .\"my script.ps1".

Frequently Asked Questions

1. What is the default execution policy in Windows?

On most Windows client versions, the default is Restricted, which prevents running scripts. On Windows Server, it’s often RemoteSigned.

2. Can I run a PowerShell script by double-clicking it?

By default, double-clicking a .ps1 file opens it in Notepad. To run it, right-click and select “Run with PowerShell” (if available) or run it from the PowerShell console.

3. How do I create a script that runs at startup?

Place the script in the Startup folder (shell:startup) or create a scheduled task using Task Scheduler.

4. What is the difference between .\script.ps1 and script.ps1?

The .\ explicitly tells PowerShell to look in the current directory. Without it, PowerShell searches the PATH, which usually doesn’t include the current folder.

5. Can I run PowerShell scripts on older Windows versions?

Yes, Windows PowerShell is available on Windows 7 and later. For PowerShell 7, you need to install it separately.

6. How do I make a script executable for other users?

Set the execution policy to RemoteSigned or Unrestricted on their machines, and ensure they have read permissions to the script file.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →