COPY BELOW THIS LINE
----------------------------------------------------------------------------------------
# Define the Current User's Home Folder
$userHome = [System.Environment]::GetFolderPath('UserProfile')
# Define the path to the DailyMaintenance.ps1 script
$maintenanceScriptPath = "$userHome\DailyMaintenance.ps1"
# PowerShell code to be written in DailyMaintenance.ps1
$maintenanceScriptContent = @'
# DailyMaintenance.ps1
# Script to perform system maintenance tasks such as scanning and updating
# Run Windows Update
Write-Host "Running Windows Update..."
Get-WindowsUpdate -Install -AcceptAll -IgnoreReboot
# Perform a quick scan with Windows Defender
Write-Host "Running Windows Defender Quick Scan..."
Start-MpScan -ScanType QuickScan
'@
# Write the PowerShell code to the DailyMaintenance.ps1 file
Set-Content -Path $maintenanceScriptPath -Value $maintenanceScriptContent -Force
# Set the DailyMaintenance.ps1 file to be executable
$acl = Get-Acl -Path $maintenanceScriptPath
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Everyone", "ReadAndExecute", "Allow")
$acl.SetAccessRule($rule)
Set-Acl -Path $maintenanceScriptPath -AclObject $acl
# Create a Scheduled Task to run the DailyMaintenance.ps1 script at 6 AM daily
$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-File `"$maintenanceScriptPath`""
$trigger = New-ScheduledTaskTrigger -Daily -At 6:00AM
$principal = New-ScheduledTaskPrincipal -UserId "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -DontStopOnIdleEnd -AllowHardTerminate -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 1)
# Register the task
Register-ScheduledTask -TaskName "6am Scan and Update" -Action $action -Trigger $trigger -Principal $principal -Settings $settings -Description "Daily system scan and update task"
Write-Host "Scheduled task '6am Scan and Update' has been created and will run at 6 AM daily."
Write-Host "The script DailyMaintenance.ps1 has been written to $maintenanceScriptPath and set to executable.
--------------------------------------------------------------------------------------
COPY ABOVE THIS LINE
Let's delve into the key components of this PowerShell script to understand its benefits.
1. Dynamic Home Folder Access
The script uses the `GetFolderPath` method to dynamically determine the current user's home directory. This ensures flexibility and adaptability across different user environments.
2. Creating and Writing to the Maintenance Script
The script dynamically constructs the path to a file named DailyMaintenance.ps1 within the user's home directory. Using `Set-Content`, it writes predefined maintenance tasks to this file. This part ensures that the maintenance tasks are readily available and customizable.
3. Making the Script Executable
The script modifies the file access control list (ACL) to give execute permissions to everyone. This step ensures that the maintenance script can run without permission issues, simplifying execution.
4. Scheduling the Script
Automating the script to run daily at 6 AM (a time likely when the system isn't in active use) ensures that maintenance tasks are performed regularly without manual intervention. This scheduled task is designed to run with the highest privileges, enabling it to carry out tasks that may require administrative rights.
Key Benefits of This Automated Approach
1. The script ensures that system updates and scans occur daily without fail.
2. Regular system updates and quick scans from Windows Defender help maintain the security posture of your system, mitigating vulnerabilities and potential threats.
3. Routine maintenance tasks can help in identifying potential system slowdowns early, ensuring that your system remains in peak performance.
4. The script can be easily customized to include additional tasks or modified to suit specific requirements. You can expand its functionality to include disk cleanup, defragmentation, or other important tasks.
5. By dynamically identifying the user’s home directory and granting necessary permissions, this script can be deployed across various user environments with minimal adjustments.
Automating system maintenance using PowerShell provides an efficient, reliable, and secure way to keep your system running smoothly. The given script encapsulates the essential tasks and schedules them to run without user intervention, making it a powerful tool in your system administration toolkit. So, why not take advantage of this script and ensure that your computer receives the care it deserves, every single day at 6 AM?