I think we may have tracked down a wrong path here.
To be clear, are you trying to delete user accounts or remove their profiles ? Those are two different things.
The “net.exe” command will delete the user account entirely. I’m guessing this isn’t what you want.
For the User Profile, there are a couple of complications -
- The profile needs to be deleted from the disk
- The profile needs to be de-referenced from HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProfileList (or else the user may get a “Profile not available” message next time they log on ,and get logged with a TEMP profile instead)
- Using ntuser.dat’s modification time is not consistent with finding their last logon time.
To check the profile’s last used time, I’d use a WMI query instead.
items 0 of (sid (string value of property "sid" of it) as string | string value of property "sid" of it, time value of property "LastUseTime" of it) whose (now - item 1 of it > 2 * day) of (select objects "* from Win32_UserProfile" of wmi) whose (set of ("S-1-5-20";"S-1-5-19";"S-1-5-18") does not contain string value of property "sid" of it)
That should give the user names. It would be possible to returning also the ‘component string of sid’ which should align with the Registry’s entry for ProfileList. Then you need to use the path given in the Registry to delete the actual profile, i.e. what if there are multiple domains or similar-named accounts, so you end up with C:\Users\User1 , C:\Users\User1.DOMAIN, etc. and the folder name doesn’t directly match the username? You have to pull the path from the Registry instead.
I went down that process before, and it’s definitely no fun. Instead, what I much prefer is configuring Windows itself to remove old profiles, using Local Group Policy. With this policy in place, Windows will delete Profiles that are more than 2 days unused (the actual cleanup occurs whenever the system reboots, so they can stack up for a while between restarts):
begin prefetch block
add prefetch item name=LGPO.zip sha1=0c74dac83aed569607aaa6df152206c709eef769 size=815660 url=https://download.microsoft.com/download/8/5/C/85C25433-A1B0-4FFA-9429-7E023E7DA8D8/LGPO.zip sha256=6ffb6416366652993c992280e29faea3507b5b5aa661c33ba1af31f48acea9c4
// Download UnZip utility
add prefetch item name=unzip.exe sha1=e1652b058195db3f5f754b7ab430652ae04a50b8 size=167936 url=http://software.bigfix.com/download/redist/unzip-5.52.exe sha256=8d9b5190aace52a1db1ac73a65ee9999c329157c8e88f61a772433323d6b7a4a
collect prefetch items
end prefetch block
// Add LGPO.zip to the client utility cache
utility __Download\LGPO.zip
// Add unzip.exe to the client utility cache
utility __Download\unzip.exe
waithidden __Download\unzip.exe -o "{pathname of client folder of current site}\__Download\LGPO.zip" -d "{pathname of client folder of current site}\__Download"
action uses wow64 redirection false
delete __createfile
createfile until EOF_EOF_EOF
; ----------------------------------------------------------------------
; PARSING COMPUTER POLICY
; Source file: registry.pol
Computer
Software\Policies\Microsoft\Windows\System
CleanupProfiles
DWORD:2
; PARSING COMPLETED.
; ----------------------------------------------------------------------
EOF_EOF_EOF
delete regpol.txt
move __createfile regpol.txt
waithidden __Download\LGPO.exe /t regpol.txt
// Regardless of whether we try to force a GPUpdate, the registry may not update until reboot
runhidden gpupdate.exe /target:computer /wait:0