Mundy

IT Knowledge Base

User Tools

Site Tools


Sidebar

Contact me at dan@mundy.co for any feedback or suggestions.


My other sites:

Search all my sites:

script_to_delete_non-profile_folders_in_users

Script to delete non-profile folders in C:\Users

The aim of this script is to delete any folder from C:\Users that does not contain NTUSER.DAT file. The specific case was that a remote desktop server has User Profile Disks enabled, but some files were being saved to C:\Users\username, even when the UPD was not mounted, so that next time the UPD was mounted, it would be C:\Users\username.000. The offending files in C:\Users\username are often the Outlook OST file. Assuming these were put here when a user logged on with a TEMP profile.




User-Profile-Directory-Cleanup.ps1
 # This script moves, out of C:\Users, any folder that is determined not to be a user profile
 
$path = "C:\Users"
$moveto = "C:\Users_Moved"
$all_loc = Get-ChildItem -Directory $path
 
foreach ($x in $all_loc){
    $z = (Join-Path $x.FullName "NTUSER.dat") # variable to hold full path + file name to search
    $test = Test-Path "$z" -PathType leaf;
    if ($test -eq 0){
        if ($x.Name -ne "Default" -and $x.Name -ne "Public"){
            if (!(Test-Path $moveto)) { New-Item -ItemType Directory $moveto }
            Move-Item C:\Users\$x $moveto\$((Get-Date).ToString('yyyyMMdd'))-$x ;
        }
    }
}
script_to_delete_non-profile_folders_in_users.txt · Last modified: 2018/04/09 09:56 (external edit)