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
- Open PowerShell – Press
Win, typePowerShell, and select Windows PowerShell or PowerShell 7 (if installed). For scripts that need admin rights, right-click and choose Run as administrator. - Check the execution policy – Run
Get-ExecutionPolicy. If it returnsRestricted, you cannot run scripts yet. To change it, runSet-ExecutionPolicy RemoteSigned(allows local scripts and signed remote scripts). You may need to confirm withY. This change is safe for most users. - 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.ps1extension, e.g.,myscript.ps1. Use UTF-8 with BOM encoding if you have non-ASCII characters. - Navigate to the script’s folder – In PowerShell, use
cd C:\path\to\folderto go to where you saved the script. - Run the script – Type
.\myscript.ps1and press Enter. The.\tells PowerShell to look in the current directory. If you get an error about execution policy, revisit step 2. - Run with parameters (if any) – If your script accepts parameters, pass them after the filename, e.g.,
.\myscript.ps1 -Name "John". - 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 – Runningmyscript.ps1without.\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
.txtor.ps1.txtwill 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
$PSScriptRootto 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-HostorWrite-Outputto display progress. - Set execution policy for the current user only – Use
Set-ExecutionPolicy -Scope CurrentUser RemoteSignedto 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 CurrentUserand 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-Hoststatements to see where it’s failing. Check for syntax errors withGet-Contentor 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
- How to Enable Desktop Slideshow in Windows 10
- BattlEye Service Failed: Test Signing Fix
- Chrome Remote Desktop on Chromebook: Setup Guide
- App Store Missing on iPad? Fix It
- How to Unroot Samsung Galaxy Core LTE
- Ways to Create More Disk Space in Windows 10