Public/Invoke-SystemMaintenance.ps1
<#
.SYNOPSIS Performs a series of system maintenance tasks including service management, disk cleanup, defragmentation, and system updates. .DESCRIPTION This function orchestrates a series of system maintenance operations including starting and stopping services, cleaning up the disk, defragmenting the disk, clearing event logs, managing scheduled tasks, and updating group policies. It concludes with setting the execution policy to restricted and scheduling a shutdown. .EXAMPLE Invoke-SystemMaintenance Runs the complete set of maintenance tasks sequentially. .NOTES Requires administrative privileges for most operations. #> function Invoke-SystemMaintenance { [CmdletBinding()] param () # Ensure script is running as Administrator if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Write-Warning "Please run this script as an Administrator!" return } # Starting necessary services "bits", "wuauserv", "defragsvc" | ForEach-Object { Write-Host "Starting $_ service and setting it to automatic startup" -ForegroundColor Green Get-Service -Name $_ | Set-Service -StartupType Automatic -PassThru | Start-Service } # Removing directories Write-Host "Removing temporary directories" -ForegroundColor Green "C:\Temp\*", "C:\Windows\Build\*", "C:\Users\Admin\Desktop\*" | ForEach-Object { Remove-Item $_ -Force -Recurse -ErrorAction SilentlyContinue } # Disk Cleanup Write-Host "Running Disk Cleanup" -ForegroundColor Green $volumeCaches = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches" foreach ($key in $volumeCaches) { New-ItemProperty -Path "$($key.PSPath)" -Name StateFlags0099 -Value 2 -Type DWORD -Force } Start-Process "$env:SystemRoot\System32\cleanmgr.exe" -ArgumentList "/sagerun:99" -Wait $volumeCaches | ForEach-Object { Remove-ItemProperty -Path "$($_.PSPath)" -Name StateFlags0099 -Force } # Disk Defragmentation Write-Host "Running Disk Defragmentation" -ForegroundColor Green Start-Process "defrag.exe" -ArgumentList "C: /H /V" -Wait # Clearing Event Logs Write-Host "Clearing Event Logs" -ForegroundColor Green Get-WinEvent -ListLog * -Force | ForEach-Object { Wevtutil.exe cl $_.LogName } # Stopping services and setting them to disabled "bits", "wuauserv", "defragsvc" | ForEach-Object { Write-Host "Stopping $_ service and setting it to disabled startup" -ForegroundColor Green Get-Service -Name $_ | Stop-Service -PassThru | Set-Service -StartupType Disabled } # Deleting Scheduled Task Write-Host "Deleting Scheduled Task if it exists" -ForegroundColor Green $taskExists = Get-ScheduledTask | Where-Object {$_.TaskName -like "GpUpdate"} if ($taskExists) { schtasks /delete /tn "GpUpdate" /f } # Set Execution Policy to Restricted Write-Host "Setting Execution Policy to Restricted" -ForegroundColor Green Set-ExecutionPolicy Restricted -Force # Run gpupdate and schedule a system shutdown Write-Host "Running GPUpdate and scheduling shutdown" -ForegroundColor Green gpupdate /force shutdown /s /t 5 } |