πŸ–₯️
David Jenner's IT Notes
  • Command line and Powershell Tools
    • Troubleshoot with CMD
    • πŸš€Troubleshoot with CMD & Powershell
    • πŸ”Analysis
    • ⚑Automation
    • πŸƒβ€β™‚οΈExample Excerises
  • Microsoft Azure/Entra
    • Azure notes
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Command line and Powershell Tools

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! πŸš€

PreviousAnalysisNextExample Excerises

Last updated 4 months ago

Was this helpful?

⚑