Deleting Old User Profiles

This topic contains 7 replies, has 5 voices, and was last updated by  H Man 3 years, 1 month ago.

  • Author
    Posts
  • #17234

    H Man
    Participant

    I would like to delete any user profiles older than 30 days prior to backing up a client system. I am getting an access denied when trying to remove the c:\users\jsmith\AppData\Local\Application Data' directory. Also is there any way to delete all the registry info associated with that user?

    I have to use powershell v2

    Here is what i have so far ... thanks

      
    [cmdletbinding(SupportsShouldProcess)]
     
    Param(
    [Parameter(Position=0)]
    [ValidateNotNullorEmpty()]
    [int]$Days= 30
    )
    
    
     
    Write-Warning "Filtering for user profiles older than $Days days" 
    
    $profile = gwmi  win32_userprofile  | ? {$_.localpath -like "*c:\users\*" -and  $_.localpath -notlike "C:\Users\Administrator"}  | 
    select  localpath,@{ Name = "lastUseTime" ; Expression = { $_.Converttodatetime( $_.LastuseTime ) } } | 
    Where {$_.LastUseTime -lt $(Get-Date).AddDays(-$days)} 
    
    if ($profile -eq $null){Write-Host  "No user profiles older than $Days days where found " 
    }
    else {
    remove-item $profile.localpath -Recurse -Force 
    }
     
  • #17235

    Raymond Slieff
    Participant

    If you are using Windows Vista or higher, and Server 2008R2 or up, you can do it like this:

    ( Get-WmiObject -Class Win32_UserProfile | Where-Object { $_.LocalPath -eq 'c:\users\user' } ).Delete()

    There is a lot of fuss about if the .Delete method exists or not, it is not in the MSDN, however I just did it on a test 2008R2 machine and it worked as expected.

  • #17236

    H Man
    Participant

    That method is not working for me

  • #17256

    Daniel Krebs
    Moderator

    Deleting user profiles is a tricky business. I believe PowerShell is not the right tool for the job. We're using Helge Klein's Delprof2 tool at work which does exactly this job very well without issues.

  • #17266

    John K
    Participant

    Like Daniel I use delprof but my friend gave me the script found here http://learn-powershell.net/2011/10/05/powershell-profile-removal-gui/. I gave it a go and for me it was temperamental but it did work.

  • #17268

    H Man
    Participant

    ok thx guys im gonna check it out

  • #17286

    Rob Simmers
    Participant

    Just so you understand the problem with your script. You are generating a PSObject from Get-WMIObject, so the object doesn't have a .Delete() method (even if it did, it would just manipulate the PSObject, not the profiles). As soon as you use Select-Object, you are generating a PSObject. That is why the example Raymond provided worked using an implicit foreach (available in v3 and above). If you are using Powershell V2, you would have to explicitly use a foreach:

    Get-WmiObject -Class Win32_UserProfile | Where-Object { $_.LocalPath -eq 'c:\users\user'} | foreach{$_.Delete()}

    To help you understand further, using your $profile variable above, do:

    $profile | foreach{ $_.GetType()}

    and then try:

    Get-WmiObject -Class Win32_UserProfile | Where-Object { $_.LocalPath -eq 'c:\users\user'} | foreach{$_.GetType()}

    You should also look at some of the Win32_UserProfile properties such as Special (indicates a SYSTEM, NETWORK, ADMINISTRATOR profile) and Loaded (indicates a profile is loaded in registry) to do your filtering. You are specifically looking for "Administrator" and not other system profiles in your filter plus it should be done in WMI filtering, not after. See what is returned with this:

    gwmi Win32_UserProfile -Filter "Loaded=False And Special=False"

    Lastly, I've had issues deleting profiles in Terminal Server environments because of long path\files which AppData contains temporary internet files that I usually find the culprit. Attached is a script that I wrote for our Citrix\TS folks and it seemed to work without issue and provided logging indicating why a profile was skipped or deleted. The script is writted to support -WhatIf, so make sure you test, then test, test again in a non-production environment:

    PS C:\> C:\Scripts\Delete-Profile.ps1 -WhatIf
  • #17293

    H Man
    Participant

    Thx Rob I'm going to check it out ill let you know

You must be logged in to reply to this topic.