What a PowerShell script is and why you would write one
A PowerShell script is a text file containing a series of commands that Windows runs one after another, without you typing each one. Instead of opening File Explorer, navigating to a folder, and deleting old files by hand every month, you write those steps once in a script and run it whenever you need to. PowerShell is built into Windows 10 and Windows 11, so you do not need to install anything.
The main reason to write a script is to save time on tasks you do the same way repeatedly. If you back up the same folders every week, check which programs are installed on your computer monthly, or clean out a Downloads folder that fills up constantly, a script handles that work in seconds instead of minutes. Scripts also reduce mistakes — once you get the steps right, they run the same way every time.
A script is also safer than clicking through unfamiliar software. You can read exactly what the script does before you run it, and you control when it runs. This matters when you are automating something that touches important files or settings.
Key Takeaways
- PowerShell scripts are text files you create in Notepad or any plain-text editor, saved with a .ps1 file extension.
- You write commands in the order you want them to run, using the same PowerShell commands you would type by hand.
- Before running any script, you may need to change your execution policy, which is a Windows setting that controls whether scripts can run at all.
- Start with a single task — like listing files in a folder or deleting files older than 30 days — before combining multiple commands into one script.
- You can schedule a script to run automatically on a set day and time using Windows Task Scheduler, so you do not have to remember to run it yourself.
Opening PowerShell and testing commands before you write a script
Before you write anything down, test your commands in PowerShell itself. This way you know they work before you put them in a script. Open PowerShell by pressing the Windows key, typing "PowerShell", and clicking "Windows PowerShell" (not "ISE" or "Core" — just the plain version). A blue window opens with a prompt that looks like PS C:\Users\YourName>.
Type a straightforward command to see how it works. For example, type Get-ChildItem C:\Users\YourName\Downloads and press Enter. PowerShell lists all the files in your Downloads folder. If you want to see only files older than 30 days, type Get-ChildItem C:\Users\YourName\Downloads | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} and press Enter. The vertical bar (|) sends the results from the first command to the second command, which filters them.
Once you have a command that does what you want, you are ready to put it in a script. Write down the exact command — copy and paste it if you can — so you do not mistype it.
Creating and saving a PowerShell script file
Open Notepad (press the Windows key, type "Notepad", and click it). Type your PowerShell commands in the order you want them to run, one per line. For a script that lists old files in your Downloads folder and then deletes them, you might write:
Get-ChildItem C:\Users\YourName\Downloads | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | Remove-Item
That single line does three things: it finds files older than 30 days, sends those results to the next command, and deletes them. If you want to see what will be deleted before the script actually deletes anything, write two lines instead:
Get-ChildItem C:\Users\YourName\Downloads | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} Get-ChildItem C:\Users\YourName\Downloads | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | Remove-Item
Run the first line, look at the results, and if they are correct, run the second line. Now save the file. Click File, then Save As. Change the filename to something descriptive like DeleteOldDownloads.ps1. The .ps1 extension tells Windows this is a PowerShell script. Choose a folder you will remember — your Documents folder is a good choice. Make sure "Save as type" is set to "All Files" so Notepad does not add .txt to the end.
Changing your execution policy so scripts can run
Windows has a safety setting called the execution policy that prevents scripts from running unless you say they can. You need to change this setting once on your computer. Open PowerShell as an administrator: press the Windows key, type "PowerShell", right-click "Windows PowerShell", and click "Run as administrator". A window opens asking if you want to allow this app to make changes — click Yes.
In the blue PowerShell window, type Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser and press Enter. PowerShell asks if you want to change the execution policy. Type Y and press Enter. This setting allows scripts you write yourself to run, but still blocks scripts downloaded from the internet unless you specifically allow them. You only have to do this once.
If you ever need to check what your execution policy is set to, type Get-ExecutionPolicy and press Enter. PowerShell shows you the current setting.
Running your script from PowerShell
Open PowerShell (not as administrator this time — regular PowerShell is fine). Navigate to the folder where you saved your script. If you saved it in Documents, type cd C:\Users\YourName\Documents and press Enter. Then type .\DeleteOldDownloads.ps1 (or whatever you named your script) and press Enter. PowerShell runs the commands in your script in order.
If something goes wrong, PowerShell shows you an error message. Read it carefully — it usually tells you which line had the problem and why. Common mistakes include typing a folder path that does not exist, using the wrong variable name (like $_ instead of $file), or forgetting a closing bracket or parenthesis. Go back to Notepad, fix the mistake, save the file, and run it again.
If your script runs without errors but does not do what you expected, go back to PowerShell and test each command by hand again. One command at a time, type it in, press Enter, and look at the results. Once you find which command is not working the way you thought, fix it in your script.
Scheduling your script to run automatically
Once your script works, you can set Windows to run it on a schedule so you do not have to remember. Open Task Scheduler by pressing the Windows key, typing "Task Scheduler", and clicking it. On the right side, click "Create Basic Task". Give it a name like "Delete Old Downloads" and click Next.
Choose when you want it to run — Daily, Weekly, or Monthly. Click Next. Set the day and time. For example, if you want it to run every Sunday at 2 AM, choose Weekly, pick Sunday, and set the time to 02:00. Click Next. Choose "Start a program" and click Next. In the "Program/script" field, type powershell.exe. In the "Add arguments" field, type the full path to your script in quotes, like -File "C:\Users\YourName\Documents\DeleteOldDownloads.ps1". Click Next, then Finish.
Windows now runs your script on the schedule you set. If you want to test it before waiting for the scheduled time, right-click the task in Task Scheduler and click "Run". The script runs when ready. Check that it did what you expected, then you can trust it to run on its own.
Starting straightforward and building from there
Your first script should do one thing well. Do not try to write a script that backs up files, checks for updates, and cleans temporary folders all at once. Start with backing up files. Get that working, test it, schedule it if you want to. Then write a second script for checking updates. This way, if something breaks, you know which script caused the problem.
As you get more comfortable, you can add features. You might add a line that sends you an email when the script finishes, or a line that logs what the script did to a text file so you can check later. PowerShell has thousands of commands available, but you do not need to learn them all. Learn the ones that solve the problems you actually have.
Frequently Asked Questions
What if I get an error that says "cannot be loaded because running scripts is disabled"?
You skipped the execution policy step. Open PowerShell as administrator again and run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser. Type Y when it asks to confirm. Then try running your script again.
Can I edit a script after I have already created it?
Yes. Right-click the .ps1 file, click "Open with", and choose Notepad. Make your changes, save the file, and run it again. You do not need to change the execution policy again — that is a one-time setting.
What happens if my script tries to delete a file that is currently open?
PowerShell shows an error and skips that file. The script continues with the next command. If you want the script to stop instead of continuing, you can add error-handling commands, but that is a more advanced topic. Start by making sure the files your script touches are not in use.
Is it safe to run scripts I find online?
Read the script in Notepad first. If you understand what every line does and it looks safe, you can run it. If you do not understand what it does, do not run it. Never run a script from a source you do not trust. When in doubt, ask someone who knows PowerShell to read it first.