locked
Script to Delete Windows 7 User Profiles on Startup RRS feed

  • Question

  • I work in a school and we need to be able to delete pupils local profiles on startup through group policy. We currently have a script that performs this for our XP client but not windows 7. I also need to be able to specify user accounts to ignore.

    Can anyone help?

    Thursday, November 25, 2010 10:24 AM

Answers

All replies

  • Hi Chris,

    Normally in XP you could use delprof.exe

    http://support.microsoft.com/kb/315411

    ...however you can't specify a particular profile to delete and it's not supported on Vista\Windows 7. I found a useful utility called "remprof.exe" which can also exclude profiles from deletion. You can read about it here:

    http://www.ctrl-alt-del.com.au/files/RemProf.txt

    Download it here:

    http://www.ctrl-alt-del.com.au/files/CAD_UtilPack.zip

    Works on Vista/Server 2008, may work on Windows 7...haven't tested it but may be worth a try.

    Cheers Matt :)

    Thursday, November 25, 2010 10:35 AM
  • Hi Matt,

    I'm unable to extract that zip file, apparently the folder is empty. Any ideas?

    Thanks for your swift reply btw

    Thursday, November 25, 2010 10:44 AM
  • Hi Chris,

    That's weird, I downloaded and extracted it fine from an XP VM. FYI I ran it against a Windows 7 system and it works fine. The .zip file contains multiple .zip files, one for each utility in the pack.

    Cheers Matt :)

    Thursday, November 25, 2010 11:02 AM
  • My mistake, downloaded it again, working fine now..Thanks
    Thursday, November 25, 2010 11:14 AM
  • Is there anyone else out there that has developed a script for this :D
    Thursday, November 25, 2010 12:08 PM
  • Hi Chris,

    I'll have a look at this tommorrow in my testlab for Windows 7. Note that the utility requires local admin rights so it would have to run as a GPO startup script as local system. it shouldn't be too difficult to script something up. I'll get back to you.

    Cheers Matt :)

    Thursday, November 25, 2010 12:22 PM
  • I've seen a couple threads using PowerShell and Win32_UserProfile to delete profiles. Haven't tried any of it, but it looks interesting.

    http://serverfault.com/questions/195043/windows-7-delprof-replacement-for-deleting-profiles

    http://stackoverflow.com/questions/3755741/delete-local-windows-profile-with-powershell

     

    • Marked as answer by IamMred Sunday, December 5, 2010 4:58 AM
    Thursday, November 25, 2010 1:45 PM
  • SET rootKey=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList

    CLS

    :check_users
        FOR /F "DELIMS=" %%i in ('DIR "C:\USERS" /B /AD') DO CALL :remove_old_profiles "%%i"
        GOTO :eof

    :remove_old_profiles

        SET dirName=%~1
        
        :custom
        IF /I "%dirName%" EQU "Default" GOTO:eof
        IF /I "%dirName%" EQU "Default User" GOTO:eof
        IF /I "%dirName%" EQU "Public" GOTO:eof
        IF /I "%dirName%" EQU "All Users" GOTO:eof
        IF /I "%dirName%" EQU "%USERNAME%" (
            ECHO Hey! You can't delete yourself, Mr. or Ms. %USERNAME%.
            GOTO:eof
        )
        @ECHO Removing Profile List Sub Key...
        FOR /F "DELIMS=" %%a IN ('REG QUERY "%rootKey%" /s /d /f C:\users\%dirName%') DO REG DELETE "%%a" /f
        @ECHO Removing C:\Users\%dirName%...
        RD /s /q "C:\USERS\%dirName%"
        RD /s /q "C:\USERS\%dirName%"
        GOTO :eof
    Tuesday, February 21, 2012 9:42 PM
  • SET rootKey=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList

    CLS

    :check_users
        FOR /F "DELIMS=" %%i in ('DIR "C:\USERS" /B /AD') DO CALL :remove_old_profiles "%%i"
        GOTO :eof

    :remove_old_profiles

        SET dirName=%~1
        
        :custom
        IF /I "%dirName%" EQU "Default" GOTO:eof
        IF /I "%dirName%" EQU "Default User" GOTO:eof
        IF /I "%dirName%" EQU "Public" GOTO:eof
        IF /I "%dirName%" EQU "All Users" GOTO:eof
        IF /I "%dirName%" EQU "%USERNAME%" (
            ECHO Hey! You can't delete yourself, Mr. or Ms. %USERNAME%.
            GOTO:eof
        )
        @ECHO Removing Profile List Sub Key...
        FOR /F "DELIMS=" %%a IN ('REG QUERY "%rootKey%" /s /d /f C:\users\%dirName%') DO REG DELETE "%%a" /f
        @ECHO Removing C:\Users\%dirName%...
        RD /s /q "C:\USERS\%dirName%"
        RD /s /q "C:\USERS\%dirName%"
        GOTO :eof
    Although it may be sound for Windows XP, this is a bad idea with Vista, Windows 7, Server 2008 and beyond.  This is not a clean method to remove profiles and will lead to Temp profiles being created on next login.  The more preferred methods I've seen and used employ PowerShell and WMI calls against "Win32_UserProfile" as mentioned above.

    dsloyer

    Thursday, August 2, 2012 6:42 PM