Cleanup-UserProfiles quickly delete unused profiles from the local machine

This is a short script that will search the local machine for profiles that haven't been used in X number of days (60 default) and deletes them. Uses ShouldProcess to provide verification of profile deletion. Can be run remotely ComputerName parameter. Also allows you to exclude

 
 
 
 
 
(0)
3,608 times
Add to favorites
Operating System
1/24/2017
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Received error
    2 Posts | Last post July 10, 2019
    • I'm trying to run the script but receiving the following error, please advise:
      
      Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
      At C:\Users\administrator\Desktop\Cleanup-UserProfiles.ps1:26 char:5
      +     $dateLastUsed = [datetime]::ParseExact(($profile.lastusetime -rep ...
      +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : FormatException
       
      Exception calling "Delete" with "0" argument(s): ""
      At C:\Users\administrator\Desktop\Cleanup-UserProfiles.ps1:48 char:9
      +         $profile.Delete()
      +         ~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : DotNetMethodException
       
      Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
      At C:\Users\administrator\Desktop\Cleanup-UserProfiles.ps1:26 char:5
      +     $dateLastUsed = [datetime]::ParseExact(($profile.lastusetime -rep ...
      +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : FormatException
      
    • Steve, the error is coming from user accounts with no LastUseTime value, you can use the following to see which accounts are causing issues, most likely default accounts that exist but have not logged in:
      
      Get-WmiObject -Class Win32_UserProfile | Where{$_.LastUseTime -eq $Null} | Select LocalPath, LastUseTime | Sort-Object LocalPath
      
      This issue doesn't appear to cause problems as the "$DateLastused -ge $DateLimit" check fails but it can cause concern seeing a load of red flying by.
      
      Possible way to get around this, if the LastUseTime is Null then set the $DateLastUsed value to be today's date/time so there is always a valid value and the profile won't be deleted.
      
      So wrap this code in an If/Else:
          #Check if profile is in date range
          $dateLastUsed = [datetime]::ParseExact(($profile.lastusetime -replace '\..+$',''),'yyyyMMddHHmmss',$null )
      
      
  • How to increase from 60 days to 365 days
    2 Posts | Last post October 15, 2018
    • Hey Shane... How to increase the duration from 60 days to 365 days
    • Nevermind... I got the option in the script... Thanks