Clean Up Storage Pre Upgrade

Just another group of tasks to add to your arsenal.  We run a Check Readiness step before our upgrades, with a minimum of 20GB Free. We have many clients that do not meet this minimum requirement and fail, and then have to remediate. While we have long term plans to automate much of this, and prevent the Task sequence from ever running on machines that don’t reach these pre-reqs, for now, a Band-Aid.

image

Step Breakdown “Storage Cleanup” Group
Run only if FreeSpace < 20GB
image

select * from win32_logicaldisk where freespace < 20000000000 and deviceid="c:"

This same query is placed on several steps.  If the step that is cleaning up is successful at getting FreeSpace above 20GB, then lets continue on.  Otherwise, the last step that will run if still under 20GB is the restart, to help flush things from the previous steps.

  1. Cleanup User Temp Folders
    image

    powershell.exe -command "Get-ChildItem 'C:\users\*\AppData\Local\Temp\*' -Recurse -Force -ErrorAction SilentlyContinue | Where-Object { ($_.CreationTime -lt $(Get-Date).AddDays(-7))} | remove-item -force -Verbose -recurse -ErrorAction SilentlyContinue"
  2. Cleanup c:\Windows\Temp
    image

    powershell.exe -command "Get-ChildItem 'C:\Windows\Temp\*' -Recurse -Force -Verbose -ErrorAction SilentlyContinue | Where-Object { ($_.CreationTime -lt $(Get-Date).AddDays(-7)) } | remove-item -force -Verbose -recurse -ErrorAction SilentlyContinue"
  3. Delete user Profiles over X Days inactive
    image

    <# 
    .SYNOPSIS 
    Use Delprof2.exe to delete inactive profiles older than X days
    tool from here: https://helgeklein.com/free-tools/delprof2-user-profile-deletion-tool
    Permission was granted for Garytown.com to redistribute in content
    .DESCRIPTION 
    Gets Top Console user from ConfigMgr Client WMI, then runs delprof tool, excluding top console user list, 
    and deletes any other inactive accounts based on how many days that you set in the -Days parameter.  
    typical arugments;
            l   List only, do not delete (what-if mode) - Set by default
            u   Unattended (no confirmation) - Recommended to leave logs
            q   Quiet (no output and no confirmation)
    
    .LINK
    https://garytown.com
    https://helgeklein.com/free-tools/delprof2-user-profile-deletion-tool - to see what arugments are available.
    #> 
    
    Param(
      [string]$Days = '365',
      [string]$argument = 'l'
    )
    $PrimaryUser = (Get-WmiObject -Namespace "root\cimv2\sms"-class sms_SystemConsoleUser).SystemConsoleUser 
    #Change the path of DelProf2.exe to where you have it.  In my package, I have it in a subfolder called StorageCleanUp
    .\StorageCleanUp\DelProf2.exe /ed:$PrimaryUser /d:$Days /$argument

    This requires that the delprof2.exe file from Helge Klein's site. (It is in the download, as I have permission to redistribute it)
    You can change the day to any number you want and run this steps as many times as you want.  My idea was that I would start at a high number, then slowly lower it, 360, 180, 90, 60, 45, 30, 20,15,10,5, and have the wmi query on it, so as soon as it dumped enough old profiles, to get 20GB Free, it could continue on leaving newer profiles.  The script also looks up the top console user, and excludes that from the deletions, so even if the primary user hasn't logged in for awhile (on leave), it won't delete that profile (as long as it's still the top console user, and not some tech's account).  This is a powerful step, be careful with it, as you could accidentally remove a profile you didn't want to. If you have some profiles you don't ever want to delete, you can exclude them by adding an /ed:user in the script (look to Helge's documentation for more info).

  4. Cleanup WinSXS folder
    image

    Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase
    
  5. CleanMgr – All Options
    image
    This was borrowed directly from stealthpuppy.  I had written something, but then found this and it was just easier to use this.
  6. Restart Computer
    The restart only happens if there still isn't 20GB Free Space.This will be added to the Task Sequence Module Download HERE.

2 thoughts on “Clean Up Storage Pre Upgrade”

  1. Thanks for the detailed post. I movedthe user profile deletion above the user temp deletion as it can make it more efficient.

    I might set this up on scheduled task for some multi-user machines to run every week.

    Reply

Leave a Reply to Andrew Porter Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.