cleanmgr.exe via Powershell


  1. Posts : 2
    Win 10
       #1

    cleanmgr.exe via Powershell


    Hello,
    I would like to hear some tips on how to fix the PowerShell script. Generally, it works fine but I want to add something like a timer that I can run a script and it will stop in 15 min anyway! (that the time, when 'clearmgr' usually will complete the task) Why I need that? Cuz I will run it via PDQ Deploy tool for more than 300 computers and it might be stuck sometimes for some reason for an unlimited time so I want to eliminate any freezes or lags.

    Please take a look at how to add the correct addition to that script. Or maybe rewrite it and do it differently?

    Thank you in advance!

    # Create reg keys
    $volumeCaches = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches"
    foreach($key in $volumeCaches)
    {
    New-ItemProperty -Path "$($key.PSPath)" -Name StateFlags0099 -Value 2 -Type DWORD -Force | Out-Null
    }

    # Run Disk Cleanup
    Start-Process -Wait "$env:SystemRoot\System32\cleanmgr.exe" -ArgumentList "/sagerun:99"

    # Delete the keys
    $volumeCaches = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches"
    foreach($key in $volumeCaches)
    {
    Remove-ItemProperty -Path "$($key.PSPath)" -Name StateFlags0099 -Force | Out-Null
    }
      My Computer


  2. Posts : 17,013
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #2

    You could add, at the start, a Start-process command to run another script in parallel and use that second script to wait 15 mins then kill the original task if it was still running.

    Start-Process - MSDocs
    Start-Process - SS64
    asynchronous - How to execute a Powershell function several times in parallel - Stack Overflow
    Start-Sleep - MSDocs
    How to end a process with PowerShell - WinAero

    Denis
      My Computer


  3. Posts : 2
    Win 10
    Thread Starter
       #3

    After a night of reflection, I decided not to change this script but find out why it hangs. Any ideas?
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 13:47.
Find Us




Windows 10 Forums