Your first PowerShell script (and how to run it on a schedule)
Write a .ps1 file, get past the 'running scripts is disabled' error the safe way, and make Windows run it automatically with Task Scheduler.
A PowerShell script is just a text file of commands saved with a .ps1 extension. If you can type a command into PowerShell, you can put it in a script.
1. Write it
Open Notepad (or VS Code) and paste:
# hello.ps1 - shows the date and the five biggest files in Downloads
Write-Host "Hello from PowerShell! It's $(Get-Date -Format 'dddd d MMMM, HH:mm')."
Get-ChildItem "$HOME\Downloads" -File |
Sort-Object Length -Descending |
Select-Object -First 5 Name, @{ n = 'Size (MB)'; e = { [math]::Round($_.Length / 1MB, 1) } }
Save it as C:\Scripts\hello.ps1. Create the folder first. If you use Notepad, set “Save as type” to All files so it doesn’t become hello.ps1.txt.
2. Run it, and fix “running scripts is disabled on this system”
In PowerShell:
cd C:\Scripts
.\hello.ps1
On a fresh Windows 10 or 11 PC you’ll probably get an error saying running scripts is disabled on this system. Microsoft’s docs explain why: Windows client editions default to the Restricted execution policy, which allows typed commands but no script files. (Windows Server defaults to RemoteSigned.)
The common fix, which Microsoft’s beginner guide also uses, is RemoteSigned for your own user. It needs no administrator rights:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
With RemoteSigned, scripts you write yourself run. Scripts downloaded from the internet must be signed, or deliberately unblocked with Unblock-File after you’ve read them. Check your settings any time with Get-ExecutionPolicy -List.
Microsoft is clear that execution policy is a safety feature, not a security boundary: it stops accidents, not attackers. Don’t set
UnrestrictedorBypassfor your whole machine just to make an error go away.
3. The same thing as a batch file
Older .bat files still work everywhere and need no execution policy. Save this as hello.bat and double-click it:
@echo off
echo Hello from a batch file! Today is %date%.
dir "%USERPROFILE%\Downloads" /o-s
pause
Batch is fine for launching programs and copying files. For anything with logic, objects or error handling, PowerShell is much easier.
4. Run it automatically with Task Scheduler
You can click through Task Scheduler → Create Task. Or register the task in one go from PowerShell:
$action = New-ScheduledTaskAction -Execute 'powershell.exe' `
-Argument '-NoProfile -ExecutionPolicy Bypass -File "C:\Scripts\hello.ps1"'
$trigger = New-ScheduledTaskTrigger -Daily -At 9am
Register-ScheduledTask -TaskName 'Hello script' -Action $action -Trigger $trigger
-ExecutionPolicy Bypasson the command line applies only to that one run, so you don’t have to loosen the machine-wide policy.- Other useful triggers:
-AtLogOnand-AtStartup(registering these usually needs an administrator prompt), and-Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 15)for “every 15 minutes”. - Test it now with
Start-ScheduledTask 'Hello script', and remove it withUnregister-ScheduledTask 'Hello script'.
You won’t see a scheduled script’s output: its window closes as soon as it finishes, and a task set to run whether or not you’re signed in has no window at all. Write to a log file instead:
"$(Get-Date -Format s) job ran" | Add-Content C:\Scripts\hello.log
Next steps
- A real-world example of scheduled PowerShell that keeps a camera wall running: Agent DVR kiosk + watchdog.
- More PowerShell, batch and scheduling links: Scripting & Automation.
Sources
- Chapter 1 - Getting started with PowerShell — by Microsoft Learn
- about_Execution_Policies — by Microsoft Learn
- Set-ExecutionPolicy — by Microsoft Learn