Hey everyone,
I have this vbscript that im testing to remove user profiles from a pc (Win7/2008 R2 DC). Now when i run it, it removes all the profiles like its suppose too and skipping the vitals (Public, all users, default, default user). The problem that arises, is that its removing the local administrator profile as well. Ive specified it with the other folders to skip it but for some reason its not.
Heres the code that i have:
Code:
const HKEY_LOCAL_MACHINE = &H80000002
set oDic=createobject("scripting.dictionary")
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
Set oWMIService = GetObject("winmgmts:\\.\root\cimv2")
set oFSO=createobject("scripting.filesystemobject")
set oShell=createobject("wscript.shell")
on error resume next
'are we on an AV machine? if so, quit - leave profiles alone in theatres
if ofso.fileexists("c:\windows\av") then wscript.quit
Set colItems = oWMIService.ExecQuery("Select * from Win32_UserProfile where special=false and loaded=false",,48)
For Each oItem in colItems
sSid=oItem.SID
Set oUserProfile = GetObject("winmgmts:{impersonationlevel=impersonate}!\\.\root\cimv2:Win32_UserProfile.SID='" & sSID &"'")
oUserProfile.Delete_
Next
'now clean up directories not attached to profiles
'and profiles not completely deleted by first step
'build a list of the directories used by profiles
sPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
oReg.EnumKey HKEY_LOCAL_MACHINE, sPath, arrSubKeys
For Each subkey In arrSubKeys
lRc=oReg.GetStringValue(HKEY_LOCAL_MACHINE, sPath & "\" & subkey ,"ProfileImagePath",sDir)
sDir=lcase(sDir)
oDic.add sDir, subkey
Next
'now add the "fixed" profiles
lRc=oReg.GetStringValue(HKEY_LOCAL_MACHINE, sPath ,"ProfilesDirectory",sRoot)
sRoot=lcase(oShell.expandenvironmentstrings(sRoot))
sRoot=sRoot & "\"
oDic.add sRoot & "public","public"
oDic.add sRoot & "all users", "all users"
oDic.add sRoot & "default","default"
oDic.add sRoot & "default user", "default user"
oDic.add sRoot & "administrator", "administrator"
set oFolder=ofso.getfolder("c:\users")
for each oSubFolder in oFolder.subfolders
sFolder=sRoot & lcase(oSubFolder.name)
if not(oDic.exists(sFolder)) then
'orphaned folder so delete it
ofso.deletefolder sFolder, true
end if
next
Any help would be greatly appreciated.