Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Scheduled scripts with Task Scheduler
📖 Scenario: You work in an office where you need to run a PowerShell script every day to check disk space and save the report. Instead of running it manually, you want to automate this using Windows Task Scheduler.
🎯 Goal: Build a PowerShell script that checks disk space on drive C: and saves the output to a file. Then, prepare a Task Scheduler command to run this script daily.
📋 What You'll Learn
Create a PowerShell script variable with disk space check command
Add a variable for the output file path
Write the command to save disk space info to the file
Show the Task Scheduler command to schedule the script daily
💡 Why This Matters
🌍 Real World
Automating routine system checks saves time and avoids manual errors. Scheduled scripts help keep systems healthy without daily manual work.
💼 Career
System administrators and IT support staff often use Task Scheduler and PowerShell scripts to automate maintenance tasks.
Progress0 / 4 steps
1
Create disk space check command
Create a variable called diskCheck that stores the PowerShell command Get-PSDrive -Name C to check disk space on drive C.
PowerShell
Hint
Use double quotes to store the command as a string in the variable diskCheck.
2
Add output file path variable
Create a variable called outputFile and set it to the string "C:\\Reports\\DiskSpaceReport.txt" which will store the disk space report.
PowerShell
Hint
Remember to escape backslashes in the file path by doubling them.
3
Write disk space info to file
Use the Invoke-Expression cmdlet with diskCheck and pipe the output to Out-File using the outputFile variable to save the disk space info to the file.
PowerShell
Hint
Use Invoke-Expression $diskCheck to run the command stored in the variable, then pipe it to Out-File with the path from outputFile.
4
Show Task Scheduler command
Write a PowerShell Write-Output statement that prints the exact command to create a daily scheduled task named DiskSpaceCheck running the script C:\\Scripts\\DiskSpaceCheck.ps1 at 9 AM every day using schtasks.exe.
PowerShell
Hint
Use Write-Output to print the full schtasks command with the correct options for daily scheduling at 9 AM.
Practice
(1/5)
1. What is the main purpose of using Task Scheduler with PowerShell scripts?
easy
A. To write scripts faster using a graphical interface
B. To convert scripts into executable files
C. To debug scripts interactively
D. To run scripts automatically at specific times without manual start
Solution
Step 1: Understand Task Scheduler's role
Task Scheduler is designed to run tasks automatically based on a schedule.
Step 2: Identify the benefit for scripts
Running scripts automatically saves time and ensures tasks run without forgetting.
Final Answer:
To run scripts automatically at specific times without manual start -> Option D
Quick Check:
Task Scheduler automates script running [OK]
Hint: Task Scheduler automates script runs on schedule [OK]
Common Mistakes:
Thinking Task Scheduler helps write or debug scripts
Confusing automation with script editing
Assuming it converts scripts to executables
2. Which of the following is the correct syntax to create a scheduled task that runs a PowerShell script daily at 7 AM using schtasks?