Public/Invoke-ClientSystemMaintenance.ps1
<#
.SYNOPSIS Performs a series of maintenance tasks on client systems, including cleaning up directories, disk cleanup, disk defragmentation, checking office license status, clearing event logs, and managing system services. .DESCRIPTION This function executes a series of system maintenance tasks specifically tailored for client machines. It includes directory cleanup, disk cleanup, disk defragmentation, optionally checking Microsoft Office license status, clearing event logs, managing Windows services, and finally, updating group policies and scheduling a shutdown. .EXAMPLE Invoke-ClientSystemMaintenance Executes all listed maintenance tasks on a client machine sequentially. .NOTES This function is intended for use on client machines. It requires administrative privileges to perform most of the operations. #> function Invoke-ClientSystemMaintenance { [CmdletBinding()] param () function Execute-Step { param ( [string]$StepDescription, [scriptblock]$Action ) Write-Host $StepDescription -ForegroundColor Green try { & $Action Write-Host "$StepDescription - Completed" -ForegroundColor Green } catch { Write-Host "$StepDescription - Failed: $_" -ForegroundColor Red } } # Step 1: Remove Directories Execute-Step "Step 1: Remove Directories" { "C:\Temp\*", "C:\Windows\Build\*", "C:\Users\Admin\Desktop\*" | ForEach-Object { Remove-Item $_ -Force -Recurse -ErrorAction SilentlyContinue } } # Step 2: Disk Cleanup Execute-Step "Step 2: Disk Cleanup" { $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 foreach ($key in $volumeCaches) { Remove-ItemProperty -Path $key.PSPath -Name StateFlags0099 -Force } } # Step 3: Disk Defragmentation Execute-Step "Step 3: Disk Defragmentation" { defrag.exe C: /H /V } # Step 4: Office License Status Check Execute-Step "Step 4: Checking Office License Status" { $osppPath = "C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs" if (Test-Path $osppPath) { cscript.exe $osppPath /dstatus Start-Sleep -Seconds 10 } else { Write-Host "Step 4: Skipping Office License Check as ospp.vbs does not exist" -ForegroundColor Yellow } } # Step 5: Clear Event Logs Execute-Step "Step 5: Clear Event Logs" { Get-WinEvent -ListLog * -Force | ForEach-Object { Wevtutil.exe cl $_.LogName } } # Step 6: Stop Services Execute-Step "Step 6: Stop Services" { Get-Service -Name "wuauserv" | Stop-Service -PassThru | Set-Service -StartupType Disabled } # Step 7: Delete Scheduled Task Execute-Step "Step 7: Deleting Scheduled Task" { $taskExists = Get-ScheduledTask | Where-Object {$_.TaskName -like "GpUpdate"} if ($taskExists) { schtasks /delete /tn "GpUpdate" /f } } # Step 8: Set Execution Policy Execute-Step "Step 8: Setting Execution Policy to Restricted" { Set-ExecutionPolicy Restricted -Force } # Step 9: Gpupdate & Shutdown Execute-Step "Step 9: Updating Group Policy and scheduling system shutdown" { gpupdate /force shutdown /s /t 5 } } |