What a PowerShell script is and why you'd write one
A PowerShell script is a text file containing a series of commands that PowerShell runs one after another. Instead of typing each command by hand, you save them in a file and run the whole sequence at once. This matters because repetitive tasks — renaming files, checking system settings, backing up folders, managing user accounts — take seconds to automate but hours to do manually every time.
PowerShell is built into Windows, so you don't need to install anything. On macOS or Linux, you can install PowerShell separately. The scripts themselves are plain text files with a .ps1 extension, which tells Windows and PowerShell that the file contains PowerShell code.
Key Takeaways
- A PowerShell script is a text file with a .ps1 extension that contains commands PowerShell runs in order.
- You write scripts in Notepad or any text editor, but Visual Studio Code with the PowerShell extension makes it easier to spot mistakes.
- Before running any script, you must change your execution policy using Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser in PowerShell as an administrator.
- Run a script by opening PowerShell, navigating to the folder where your script lives, and typing .\scriptname.ps1.
- Start with simple tasks like listing files or creating folders, then add conditions and loops once you understand the basics.
Setting up your execution policy so scripts can run
Windows blocks PowerShell scripts by default as a security measure. Before you run any script — even one you wrote yourself — you need to change your execution policy. This is a one-time setup step.
Open PowerShell as an administrator: right-click the PowerShell icon and select "Run as administrator". Then type this command and press Enter:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
PowerShell will ask you to confirm. Type Y and press Enter. This setting allows scripts you write or download from trusted sources to run, while still blocking unsigned scripts from the internet. The -Scope CurrentUser part means this applies only to your user account, not the whole computer.
You only need to do this once. After that, any .ps1 script you run will work without this step again.
Writing your first script in a text editor
Open Notepad (or any text editor) and type your first command. A simple one to start with is:
Write-Host "Hello from PowerShell"
Write-Host is a PowerShell command that prints text to the screen. The text inside the quotes is what appears when the script runs. Save this file with a .ps1 extension — for example, hello.ps1 — in a folder you'll remember, like your Documents folder.
For longer scripts or ones you'll edit often, Visual Studio Code is better than Notepad. It's free, and when you install the PowerShell extension, it highlights mistakes in your code before you run it. Download Visual Studio Code, open it, go to Extensions (the icon that looks like four squares), search for "PowerShell", and click Install on the official Microsoft extension.
Once installed, open a .ps1 file in Visual Studio Code and you'll see color-coded syntax — commands in one color, text in another — which makes spotting errors much easier.
Running your script from PowerShell
Open PowerShell (not as administrator this time, unless your script needs to change system settings). Use the cd command to navigate to the folder where you saved your script. For example, if your script is in Documents:
cd C:\Users\YourUsername\Documents
Replace "YourUsername" with your actual Windows username. Then type the script name with a dot and backslash in front:
.\hello.ps1
Press Enter. Your script runs, and you see the output. If something goes wrong, PowerShell shows an error message. Read it carefully — it usually tells you which line failed and why.
If you get an error saying the script "cannot be loaded because running scripts is disabled", you skipped the execution policy step. Go back and run that Set-ExecutionPolicy command as an administrator first.
Common commands to build scripts with
Once you understand the basics, you can combine commands to do real work. Here are the ones you'll use most often:
Get-ChildItem lists files and folders. Get-ChildItem C:\Users\YourUsername\Downloads shows everything in your Downloads folder. Get-Process lists all running programs. Stop-Process -Name notepad closes Notepad. New-Item -ItemType File -Path C:\test.txt creates a new file.
Copy-Item copies files. Copy-Item C:\source.txt C:\destination.txt copies source.txt to a new location. Remove-Item deletes files — use this carefully. Get-Content reads the contents of a text file and displays it on screen.
You can also use if statements to make decisions. For example:
if (Test-Path C:\myfile.txt) { Write-Host "File exists" } else { Write-Host "File not found" }
This checks whether a file exists and prints different messages depending on the answer. Test-Path is the command that checks if a file or folder exists.
Adding loops to repeat actions
A loop runs the same commands multiple times. The most common is a foreach loop, which repeats an action for each item in a list.
For example, this script lists every file in a folder and prints its name:
$files = Get-ChildItem C:\Users\YourUsername\Documentsforeach ($file in $files) { Write-Host $file.Name }
The first line gets all files and stores them in a variable called $files. Variables in PowerShell start with a dollar sign. The second line says "for each file in that list, print its name". The .Name part means "just the filename, not the full path".
You can also use a while loop, which repeats as long as a condition is true. This is useful when you don't know how many times you need to repeat something:
$count = 0while ($count -lt 5) { Write-Host $count; $count = $count + 1 }
This prints the numbers 0 through 4. The -lt means "less than". Each time through the loop, $count increases by 1 until it reaches 5, then the loop stops.
Troubleshooting scripts that don't work
When a script fails, PowerShell shows an error message with a line number. Open your script and look at that line first. Common mistakes include misspelling a command name, forgetting a dollar sign on a variable, or using the wrong path to a file or folder.
If the error message is unclear, copy it into a search engine along with "PowerShell" — someone has usually hit the same problem and posted the answer online. Microsoft's official PowerShell documentation is also free and searchable at learn.microsoft.com.
Test your script on a small set of files or folders first, not on your entire system. For example, if you're writing a script to delete files, test it on a test folder with just a few files before running it on important data. Once you're confident it works, you can run it on the real thing.
You can also add Write-Host commands throughout your script to print messages at each step. This helps you see where the script stops if something goes wrong. For example:
Write-Host "Starting script"Get-ChildItem C:\testWrite-Host "Got file list"
If you see "Starting script" but not "Got file list", you know the problem is in the Get-ChildItem command.
Frequently Asked Questions
Can I run a PowerShell script by double-clicking it?
Not by default — Windows will open it in Notepad instead. You must run it from PowerShell using the .\scriptname.ps1 command. If you want to double-click to run it, you can create a batch file that launches PowerShell and runs your script, but that's more advanced.
What's the difference between PowerShell and Command Prompt?
Command Prompt (cmd.exe) is older and much simpler. PowerShell is more powerful and can do things Command Prompt cannot. Scripts written for PowerShell won't work in Command Prompt, and vice versa. Always use PowerShell for .ps1 scripts.
Is it safe to run scripts I find online?
Only if you trust the source and you read the script first. Open any downloaded .ps1 file in a text editor and look at what it does before running it. If you don't understand what a command does, search for it online. Never run a script you don't understand.
Can I schedule a script to run automatically?
Yes, using Windows Task Scheduler. You can set a script to run at a specific time, when you log in, or when a certain event happens. This is more advanced, but it's how you automate tasks that need to happen regularly without you opening PowerShell each time.
What if I need to use a variable that changes?
You can pass information into a script using parameters. At the top of your script, add param($foldername). Then when you run it, type .\scriptname.ps1 -foldername "C:\MyFolder". Inside the script, $foldername will contain whatever you typed.