HELP! This script runs on Windows 7 machines in numerous computer labs across campus. The goal is to delete users and user profiles from the computers at logoff. The script executes under NT Authority\System context and is triggered by the user logoff event.
The problem occurs in the section "# delete user profile directories". It fails to delete subfolders due to the junction points in certain subfolders. I need a way to remove all the junction points from the user profile directories before I delete them.
Whoever can get this final piece working will be a scripting hero to many school administrators, who need this functionality.
Thanks.
# Clear user data
# Delete user objects from local database
$objComputer = [ADSI]("WinNT://$env:COMPUTERNAME")
$LocalUsers = ($objComputer.psbase.children |
Where-Object {$_.psBase.schemaClassName -eq "User"} |
Where-Object {$_.Name -notmatch "localadmin"}|
Where-Object {$_.Name -notmatch "Administrator"} |
Where-Object {$_.Name -notmatch "Guest"} |
Select-Object -expand Name)
foreach ($User in $LocalUsers)
{$objComputer.Delete("user",$User) }
# delete user profile registry entries
get-childitem "registry::hklm\software\microsoft\windows nt\currentversion\profileList" |
Where-Object {$_.Name -notlike "*500"} |
remove-item
# delete user profile directories
get-childitem c:\users |
Where-Object {$_.Name -notmatch "localadmin"} |
Where-Object {$_.Name -notmatch "Administrator"} |
Where-Object {$_.Name -notmatch "Public"} |
remove-item -recurse -force