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 ()

    # Step 1: Remove Directories
    Write-Host "Step 1: Remove Directories" -ForegroundColor Green
    "C:\Temp\*", "C:\Windows\Build\*", "C:\Users\Admin\Desktop\*" | ForEach-Object {
        Remove-Item $_ -Force -Recurse -ErrorAction SilentlyContinue
    }

    # Step 2: Disk Cleanup
    Write-Host "Step 2: 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
    foreach ($key in $volumeCaches) {
        Remove-ItemProperty -Path $key.PSPath -Name StateFlags0099 -Force
    }

    # Step 3: Disk Defragmentation
    Write-Host "Step 3: Disk Defragmentation" -ForegroundColor Green
    defrag.exe C: /H /V

    # Step 4: Office License Status Check
    $osppPath = "C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs"
    if (Test-Path $osppPath) {
        Write-Host "Step 4: Checking Office License Status" -ForegroundColor Green
        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
    Write-Host "Step 5: Clear Event Logs" -ForegroundColor Green
    Get-WinEvent -ListLog * -Force | ForEach-Object { Wevtutil.exe cl $_.LogName }

    # Step 6: Stop Services
    Write-Host "Step 6: Stop Services" -ForegroundColor Green
    Get-Service -Name "wuauserv" | Stop-Service -PassThru | Set-Service -StartupType Disabled

    # Step 7: Delete Scheduled Task
    Write-Host "Step 7: Deleting Scheduled Task" -ForegroundColor Green
    $taskExists = Get-ScheduledTask | Where-Object {$_.TaskName -like "GpUpdate"}
    if ($taskExists) {
        schtasks /delete /tn "GpUpdate" /f
    }

    # Step 8: Set Execution Policy
    Write-Host "Step 8: Setting Execution Policy to Restricted" -ForegroundColor Green
    Set-ExecutionPolicy Restricted -Force

    # Step 9: Gpupdate & Shutdown
    Write-Host "Step 9: Updating Group Policy and scheduling system shutdown" -ForegroundColor Green
    gpupdate /force
    shutdown /s /t 5
}