Windows 7 Thread, Deleting profiles in Technical; Originally Posted by ChrisMiles
Theres quite a lot of problems with that script, for example it re-queries wmi for a ...
-
6th April 2011, 07:51 PM #16 
Originally Posted by
ChrisMiles
Theres quite a lot of problems with that script, for example it re-queries wmi for a Win32_UserProfile object it already has before deleting it, also the code to iterate through registry entries looking for profile paths which shouldn't be deleted isn't needed because we already queried WMI for profile information. I've rewritten the script here which runs on my PC without error at least:
Code:
On Error Resume Next
Const HKEY_LOCAL_MACHINE = &H80000002
Dim objShell, objFSO, objProfilesFolder, objProfileFolder, objWMIService, objRegistry, colProfiles, objProfile, dicFoldersInUse, sProfilesFolderPath, sProfileFolderPath
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("Winmgmts:\\.\root\cimv2")
Set objRegistry = GetObject("Winmgmts:\\.\root\default:StdRegProv")
Set dicProfilePaths = CreateObject("Scripting.Dictionary")
Set colProfiles = objWMIService.ExecQuery("Select * From Win32_UserProfile")
If Err.Number <> 0 Then
WScript.Exit
End If
If Not colProfiles Is Nothing Then
For Each objProfile in colProfiles
If objProfile.Special = False And objProfile.Loaded = False And Left(objProfile.SID, 8) = "S-1-5-21" And objProfile.LocalPath <> "" And InStr(1, objProfile.LocalPath, "administrator", 1) = 0 Then
Err.Clear
objProfile.Delete_
If Err.Number = -2147024751 Then
objFSO.DeleteFolder objProfile.LocalPath, True
Else
MsgBox "Error"
End If
Else
dicProfilePaths.Add LCase(objProfile.LocalPath), objProfile.SID
End If
Next
End If
objRegistry.GetStringValue HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", "ProfilesDirectory", sProfilesFolderPath
sProfilesFolderPath = LCase(objShell.ExpandEnvironmentStrings(sProfilesFolderPath))
Set objProfilesFolder = objFSO.GetFolder(sProfilesFolderPath)
For Each objProfileFolder in objProfilesFolder.SubFolders
sProfileFolderPath = LCase(objProfileFolder.Name)
If (sProfileFolderPath <> "default" And sProfileFolderPath <> "default user" And sProfileFolderPath <> "public" And sProfileFolderPath <> "all users") Then
sProfileFolderPath = sProfilesFolderPath & "\" & sProfileFolderPath
If Not dicProfilePaths.Exists(sProfileFolderPath) Then
objProfileFolder.Delete
End If
End If
Next Thanks, can you confirm that this works on Vista / 7 and cleans the registry etc?
Also can i add a few user profiles i want to keep eg administrator?
Just want to be 100%
Last edited by FN-GM; 6th April 2011 at 07:59 PM.
-
-
IDG Tech News
-
6th April 2011, 10:08 PM #17 When i run the script its producing a message saying error. What casues this please?
Thanks
-
-
6th April 2011, 11:04 PM #18 We've not had any issues at all using delprof from the XP\Server 2003 toolkit on Windows 7 clients and Server 2008 R2 Terminal Servers. It runs every Friday at 4pm and seems to do the trick.
-
-
7th April 2011, 08:12 AM #19 
Originally Posted by
FN-GM
When i run the script its producing a message saying error. What casues this please?
Thanks
It means either you don't have permission or there are locked files in the profile folder which are preventing the folder from being deleted. This should not normally happen. There might be some issue here you need to investigate (ie why is there locked files in a profile which is not loaded).
To disable this message comment out the line:
-
-
7th April 2011, 08:13 AM #20 I know what it is. Vmware tools is putting a file in there and locking it. Done a regedit to VMware and got rid of the profiles so it should be ok now.
Thanks
-
-
9th June 2011, 01:28 AM #21
- Rep Power
- 0

Originally Posted by
FN-GM
Just to double check that will work on Windows 7? Do you know if it works on Vista?
I know someone who has Server 2008 Terminal Servers so would be handy for getting rid of allot of profiles stored on there.
Thanks
I wrote this batch script for AU - have it running as a scheduled task in all our lab and classroom machines. You can easily copy/paste profiles you need to preserve and it allows for remote logging of error reporting. This allows me to get nightly reports from our lab machines letting me know if any profiles are stuck.
Code:
@echo off
:START
FOR /f "tokens=*" %%a IN ('dir c:\USERS /b /ad') DO CALL :PATHCHECK "%%a"
GOTO REGISTRY
::The following is where you would put in the profile you wish to exclude from the wipe. Just copy/paste a line and make the appropriate revisions.
:PATHCHECK
IF /i [%1]==["Administrator"] GOTO :PATHSKIP
IF /i [%1]==["All Users"] GOTO :PATHSKIP
IF /i [%1]==["Default"] GOTO :PATHSKIP
IF /i [%1]==["Default user"] GOTO :PATHSKIP
IF /i [%1]==["public"] GOTO :PATHSKIP
GOTO PATHCLEAN
:PATHSKIP
ECHO. Skipping path clean for user %1
GOTO :EOF
:PATHCLEAN
ECHO. Cleaning profile for: %1
rmdir C:\USERS\%1 /s /q > NUL
IF EXIST "C:\USERS\%1" GOTO RETRYPATHFIRST
IF NOT EXIST "C:\USERS\%1" GOTO :EOF
:RETRYPATHFIRST
ECHO. Error cleaning profile for: %1 - Trying again.
rmdir C:\USERS\%1 /s /q > NUL
IF EXIST "C:\USERS\%1" GOTO RETRYPATHSECOND
IF NOT EXIST "C:\USERS\%1" GOTO :EOF
:RETRYPATHSECOND
ECHO. Error cleaning profile for: %1 - Trying again.
rmdir C:\USERS\%1 /s /q > NUL
GOTO :EOF
:REGISTRY
ECHO.------------
FOR /f "tokens=*" %%a IN ('reg query "hklm\software\microsoft\windows nt\currentversion\profilelist"^|find /i "s-1-5-21"') DO CALL :REGCHECK "%%a"
GOTO VERIFY
::The following is where it parses the registry data and checks it against the user path. Copy/paste the IF line and make the user modification needed.
:REGCHECK
FOR /f "tokens=3" %%b in ('reg query %1 /v ProfileImagePath') DO SET USERREG=%%b
IF /i [%USERREG%]==[c:\Users\Administrator] GOTO :REGSKIP
GOTO REGCLEAN
:REGSKIP
ECHO. Skipping registry clean for %USERREG%
GOTO :EOF
:REGCLEAN
ECHO. Cleaning registry for: %USERREG%
reg delete %1 /f
GOTO :EOF
::The cleaning portion of the script is now done. Now begins the verification and log reporting.
:VERIFY
FOR /f "tokens=*" %%c IN ('dir c:\USERS /b /ad') DO CALL :VERIFYPATH "%%c"
::Same thing as the clean - if you need to exclude an account, make your copy/paste below.
:VERIFYPATH
IF /i [%1]==["Administrator"] GOTO :EOF
IF /i [%1]==["All Users"] GOTO :EOF
IF /i [%1]==["Default"] GOTO :EOF
IF /i [%1]==["Default user"] GOTO :EOF
IF /i [%1]==["public"] GOTO :EOF
GOTO VERPATHREPORT
:VERPATHREPORT
ECHO. %1
IF /i [%1]==[] (
set PATHRESULT=PATH_SUCCESS
) ELSE (
set PATHRESULT=PATH_FAILURE
)
ECHO. %PATHRESULT%
GOTO REGVERIFY
:REGVERIFY
ECHO.------------
FOR /f "tokens=*" %%d IN ('reg query "hklm\software\microsoft\windows nt\currentversion\profilelist"^|find /i "s-1-5-21"') DO CALL :REGCHECKVERIFY "%%d"
GOTO REGVERIFYECHO
::Same thing as the registry clean - copy/paste excluded profiles below.
:REGCHECKVERIFY
FOR /f "tokens=3" %%e in ('reg query %1 /v ProfileImagePath') DO SET USERREGV=%%e
IF /i [%USERREGV%]==[c:\Users\Administrator] GOTO :EOF
GOTO REGVERIFYECHO
:REGVERIFYECHO
ECHO. %1
IF /i [%1]==[] (
set REGRESULT=REG_SUCCESS
) ELSE (
set REGRESULT=REG_FAILURE
)
ECHO. %REGRESULT%
GOTO REPORTCHECK
::The following is where you would enter the mapped drive path.
::You can use a straight UNC if you like, but I find this to be a bit
::more solid and it allows you to use different creds in case you
::automate it for a local scheduled task to run as local admin.
:REPORTCHECK
net use t: \\server\path
IF EXIST "t:\labreport.txt" (
GOTO REPORTGEN
) ELSE (
GOTO EXIT
)
::This is a time/date stamp creator that I actually pulled from a Minecraft
::to Dropbox backup script I made a long while back.
:REPORTGEN
FOR /F "tokens=1 delims=:" %%f in ('time /T') DO SET T=%%f
FOR /F "tokens=*" %%g in ('echo %date:~10,4%-%date:~4,2%-%date:~7,2% %T%-%time:~3,2%-%time:~6,2%') DO SET TDATETIME=%%g
ECHO. %PATHRESULT% %REGRESULT% %COMPUTERNAME% %TDATETIME% >> "t:\labreport.txt"
net use t: /delete
GOTO EXIT
:EXIT
exit
:EOF
-
-
9th June 2011, 01:41 AM #22 
Originally Posted by
FN-GM
I'm hoping someone will have a script one day
It can't get any simpler than this... 
Code:
DelProf2.exe /u /i /r
http://helgeklein.com/free-tools/del...deletion-tool/
- Delprof2 deletes inactive Windows user profiles (profiles that are not currently loaded).
- Delprof2 is syntax compatible with the original Delprof by Microsoft. Unlike the original it works on Windows 7 (and XP/2003/Vista/2008/2008 R2).
- If possible, Delprof2 uses the backup and restore privileges to bypass security and delete even profiles the executing user does not normally have access to.
- Delprof2 has no problem whatsoever deleting files in very long paths (longer than MAX_PATH, 260 characters).
- Delprof2 also cleans up stale ProfileList SID.bak registry entires, a common cause of temporary profiles.
-
SHARE:
Similar Threads
-
By GoldenWonder in forum Windows 7
Replies: 9
Last Post: 11th February 2010, 11:04 AM
-
By Mr_M_Cox in forum Windows
Replies: 44
Last Post: 17th March 2009, 03:02 PM
-
By Millsy79 in forum Windows
Replies: 7
Last Post: 31st July 2008, 07:40 PM
-
By pinemarten in forum General Chat
Replies: 4
Last Post: 29th August 2006, 08:44 PM
-
By Gordie in forum Scripts
Replies: 4
Last Post: 19th June 2006, 03:41 PM
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules