Created
October 23, 2017 08:12
-
-
Save AshFlaw/dc031f9bb3b02a87beb6ae4355620976 to your computer and use it in GitHub Desktop.
Disable Windows scheduled defrag on all computers in a domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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