Skip to content

Instantly share code, notes, and snippets.

@AshFlaw
Created October 23, 2017 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AshFlaw/dc031f9bb3b02a87beb6ae4355620976 to your computer and use it in GitHub Desktop.
Save AshFlaw/dc031f9bb3b02a87beb6ae4355620976 to your computer and use it in GitHub Desktop.
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