Deleting Old User Profiles

Welcome Forums General PowerShell Q&A Deleting Old User Profiles

This topic contains 7 replies, has 5 voices, and was last updated by

4 years, 7 months ago.

  • Author
  • #17234

    Points: 0
    Rank: Member

    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

    [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

    Points: 21
    Rank: Member

    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

    Points: 0
    Rank: Member

    That method is not working for me

  • #17256

    Points: 24
    Team Member
    Rank: Member

    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

    Points: 1
    Rank: Member

    Like Daniel I use delprof but my friend gave me the script found here I gave it a go and for me it was temperamental but it did work.

  • #17268

    Points: 0
    Rank: Member

    ok thx guys im gonna check it out

  • #17286

    Points: 492
    Helping Hand
    Rank: Contributor

    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

    Points: 0
    Rank: Member

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

  • #113660

    Points: 2
    Rank: Member

    Script to Remove User Profiles older than two days

    $d = (get-date).AddDays(-2)
    $prof = @(ls c:\users -Exclude "Administrator")
    foreach($i in $prof){
    if($i.lastwritetime -lt $d){
    $tobedeleted += $i }

    foreach($p in $tobedeleted){
    cmd.exe /c "rmdir /s /q $p"

The topic ‘Deleting Old User Profiles’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort