none
Remote PowerShell Defrag RRS feed

  • Question

  • I'm trying to setup remote defrag on some servers. I've checked a bunch of examples on the Google - but I don't quite understand the basic command to call to get defrag to run.

    Can someone please tell me what I am missing from this code:

    $Servers = "ServerName"
    
    ForEach ($Server in $Servers)
    {
        $Drive = Get-WmiObject -Class Win32_Volume -ComputerName $Server -Filter "DriveLetter = 'c:'"
        $Drive.Defrag()   
    }

    Thanks for any help.

    Wednesday, October 8, 2014 12:01 AM

Answers

  • $Servers = "ServerName"
    
    ForEach ($Server in $Servers)
    {
        $Drive = Get-WmiObject -Class Win32_Volume -ComputerName $Server -Filter "DriveLetter = 'c:'"
        $Drive.Defrag($true)   
    }

    Turning it to $false will avoid defrag

    Please check if defragment is really required before executing the script


    Regards Chen V [MCTS SharePoint 2010]

    • Proposed as answer by Jason Warren Thursday, October 9, 2014 12:00 AM
    • Marked as answer by Scott Wilson 85 Tuesday, October 28, 2014 7:35 AM
    Wednesday, October 8, 2014 12:51 PM

All replies

  • The defrag method has a boolean parameter

    Screenshot from

    Powershell GUI Application - WIN 32 Class Explorer
    http://gallery.technet.microsoft.com/Powershell-GUI-Application-a884a710

    Wednesday, October 8, 2014 1:07 AM
  • $Servers = "ServerName"
    
    ForEach ($Server in $Servers)
    {
        $Drive = Get-WmiObject -Class Win32_Volume -ComputerName $Server -Filter "DriveLetter = 'c:'"
        $Drive.Defrag($true)   
    }

    Turning it to $false will avoid defrag

    Please check if defragment is really required before executing the script


    Regards Chen V [MCTS SharePoint 2010]

    • Proposed as answer by Jason Warren Thursday, October 9, 2014 12:00 AM
    • Marked as answer by Scott Wilson 85 Tuesday, October 28, 2014 7:35 AM
    Wednesday, October 8, 2014 12:51 PM