β‘Automation
Hereβs an updated Automation & Scheduled Tasks section with both CMD and PowerShell commands laid out in a copy-friendly format.
π Windows Automation & Scheduled Tasks (CMD + PowerShell)
Below are useful automation tasks using both Command Prompt (CMD) and PowerShell, allowing you to schedule tasks, automate maintenance, and manage system operations efficiently.
1οΈβ£ Schedule a Task to Run Every Night
β CMD:
schtasks /create /tn "NightlyTask" /tr "C:\Scripts\cleanup.bat" /sc daily /st 23:00
β PowerShell:
$Trigger = New-ScheduledTaskTrigger -Daily -At 11PM
$Action = New-ScheduledTaskAction -Execute "C:\Scripts\cleanup.bat"
Register-ScheduledTask -TaskName "NightlyCleanup" -Trigger $Trigger -Action $Action
π Purpose: Runs a cleanup script every night at 11 PM.
π Use Case: Automating log cleanups, backups, or maintenance.
2οΈβ£ Automatically Shutdown the PC at a Specific Time
β CMD:
shutdown /s /t 3600
β PowerShell:
Start-Sleep -Seconds 3600; Stop-Computer -Force
π Purpose: Shuts down the PC after 1 hour (3600 seconds).
π Use Case: Prevents computers from staying on overnight.
3οΈβ£ Restart the PC After a Set Time
β CMD:
shutdown /r /t 1800
β PowerShell:
Start-Sleep -Seconds 1800; Restart-Computer -Force
π Purpose: Restarts the PC after 30 minutes (1800 seconds).
π Use Case: Used after installing software updates.
4οΈβ£ Schedule a System Reboot Every Sunday at 2 AM
β CMD:
schtasks /create /tn "WeeklyReboot" /tr "shutdown /r /f /t 0" /sc weekly /d SUN /st 02:00
β PowerShell:
$Trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday -At 2AM
$Action = New-ScheduledTaskAction -Execute "shutdown.exe" -Argument "/r /f /t 0"
Register-ScheduledTask -TaskName "WeeklyReboot" -Trigger $Trigger -Action $Action
π Purpose: Forces a system reboot every Sunday at 2 AM.
π Use Case: Ensures servers or workstations stay refreshed.
5οΈβ£ Empty the Recycle Bin Automatically
β PowerShell:
Clear-RecycleBin -Force
π Purpose: Deletes all items in the Recycle Bin.
π Use Case: Helps free up disk space automatically.
6οΈβ£ Uninstall a Program Silently
β CMD:
wmic product where "name='Program Name'" call uninstall /nointeractive
β PowerShell:
(Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -eq "Program Name" }).Uninstall()
π Purpose: Removes a program silently without user prompts.
π Use Case: Useful for mass uninstallations.
7οΈβ£ Create a Backup of a Folder Daily
β CMD:
xcopy C:\ImportantFiles D:\Backup /E /I /Y
β PowerShell:
Copy-Item -Path "C:\ImportantFiles\*" -Destination "D:\Backup" -Recurse -Force
π Purpose: Backs up an entire folder to another drive.
π Use Case: Automates file backups.
8οΈβ£ Disable Windows Defender Temporarily
β PowerShell:
Set-MpPreference -DisableRealtimeMonitoring $true
π Purpose: Temporarily disables Windows Defender real-time protection.
π Use Case: Useful when installing trusted third-party software.
9οΈβ£ Enable Windows Defender Again
β PowerShell:
Set-MpPreference -DisableRealtimeMonitoring $false
π Purpose: Re-enables Windows Defender after temporary disabling.
π One-Click Full System Cleanup
β PowerShell:
Write-Host "Starting System Cleanup..."
Start-Process "cleanmgr.exe" -ArgumentList "/sagerun:1" -NoNewWindow -Wait
Clear-RecycleBin -Force
Get-ChildItem "C:\Windows\Temp" -Recurse | Remove-Item -Force -Recurse
Write-Host "Cleanup Complete!"
π Purpose: Runs a disk cleanup, empties the Recycle Bin, and deletes temp files.
π Use Case: Automates cleanup tasks for better performance.
π How to Use These Scripts
1. For CMD: Open Command Prompt (cmd.exe) as Administrator and paste the command.
2. For PowerShell: Open PowerShell as Administrator and paste the script.
3. For Automation: Add PowerShell scripts to Task Scheduler to run them automatically.
π₯ These automation scripts save time, optimize performance, and improve system efficiency! Let me know if you need more! π
Last updated
Was this helpful?