Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Disable Windows scheduled defrag on all computers in a domain
$ErrorActionPreference = "Continue"
$computers = Get-ADComputer -Filter 'ObjectClass -eq "Computer"' | Select-Object -Expand DNSHostName
Function DisableDefrag
{
If ((Get-ScheduledTask -TaskName 'ScheduledDefrag').State -eq 'Ready')
{
    Disable-ScheduledTask -TaskName 'ScheduledDefrag' -TaskPath '\Microsoft\Windows\Defrag'
Write-Output "$env:computername Scheduled defrag disabled"
}
Else
{
Write-Output "$env:computername Scheduled defrag not enabled"
}
}
Foreach ($Computer in $Computers)
{
Write-Output "Processing $Computer"
winrm id -r:$Server 2>$null | Out-Null
if ($LastExitCode -eq 0)
{
Invoke-Command -ComputerName $Computer -ScriptBlock ${function:DisableDefrag}
}
Else
{
Write-Output "Computer $Computer not online"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.