4

I've been working on a script that is supposed to run cleanmgr.exe silently when disk C: is less than a 1gb, it's all working well, but one thing I couldn't achieve...

I want to run cleanmgr.exe silently fully! I don't wanna see anything from the disk cleanup gui not even a screen that says done.

using sageset and sagerun made it automated yes, but it still shows the process.

Here's my code (I know it has some issues regarding few things, but I'm focusing on the silent execution here, thanks!):

if ($freeSpace -le $lowSpace)
{   
   cleanmgr.exe /sagerun:10
   defrag C:
}

else
{
   echo "sorry!"
   pause
}
AliAlghamdi
  • 43
  • 1
  • 1
  • 4

5 Answers5

5

Actually cleanmgr /verylowdisk runs silently.

cleanmgr /verylowdisk is like a version of cleanmgr /lowdisk with no user interaction.

ToinoBiclas
  • 262
  • 4
  • 13
  • "Actually cleanmgr /verylowdisk runs silently." MAYBE so, but tit still hangs the prompt in PowerShell. You need to call it as a job. – Patrick Burwell Sep 15 '22 at 21:33
1

If you navigate to C:\windows\system32\cleanmgr.exe /? in the command prompt you will see the switches for the exe. Unfortunately, it looks like there are no silent switches for this utility.

enter image description here

runcmd
  • 572
  • 2
  • 7
  • 22
0

The best way I found to do this is make a Scheduled task with schtasks (I built an XML config that gets deployed) and run the task on creation, in the settings it can be set to run as system/admin (which you would need for a defrag) and non interactively. The only issue I have had is the Windows update cleanup component doesn't seem to run.

0
#Runs Disk Cleanup with the /VERKLOWDISK argument to clean as much as possible.
Start-Process -FilePath cleanmgr.exe -ArgumentList '/VERYLOWDISK'
#Checks once per second for the popup window that is displayed upon cleanup completion.
#Must be run in an interactive session in order for this to work.    
While ((Get-Process -Name cleanmgr).MainWindowTitle -ne "Disk Space Notification") {Start-Sleep 1}
#Kills Disk Cleanup once completion popup is detected.
Stop-Process -Name cleanmgr -Force -ErrorAction SilentlyContinue
IMS
  • 1
  • 1
  • Hey IMS, welcome to Stack Overflow and thank you for your answer! The community would appreciate if to addition to the provided code snippet you would give more context on how it works. – mingaleg Feb 06 '23 at 22:00
-1

So not sure where your at with this, but this is what I'm using to clean up templates that we are building in Packer prior to deploying in VMware.

$strKeyPath   = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches"
$strValueName = "StateFlags0065"
$subkeys      = Get-ChildItem -Path $strKeyPath -Name

ForEach($subkey in $subkeys){
    $null = New-ItemProperty `
        -Path $strKeyPath\$subkey `
        -Name $strValueName `
        -PropertyType DWord `
        -Value 2 `
        -ea SilentlyContinue `
        -wa SilentlyContinue
}

Start-Process cleanmgr `
        -ArgumentList "/sagerun:65" `
        -Wait `
        -NoNewWindow `
        -ErrorAction   SilentlyContinue `
        -WarningAction SilentlyContinue

ForEach($subkey in $subkeys){
    $null = Remove-ItemProperty `
        -Path $strKeyPath\$subkey `
        -Name $strValueName `
        -ea SilentlyContinue `
        -wa SilentlyContinue
}

exit 0