+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
Windows 7 Thread, Deleting profiles in Technical; Every half term I run a delprof script to delete profiles on xp and leave specified profiles, is there anything ...
  1. #1
    chrbb's Avatar
    Join Date
    Oct 2005
    Location
    Midlands
    Posts
    1,509
    Thank Post
    141
    Thanked 67 Times in 62 Posts
    Rep Power
    47

    Deleting profiles

    Every half term I run a delprof script to delete profiles on xp and leave specified profiles, is there anything similar for windows 7?

  2. #2

    witch's Avatar
    Join Date
    Nov 2005
    Location
    Dorset
    Posts
    11,518
    Thank Post
    1,532
    Thanked 2,636 Times in 1,825 Posts
    Rep Power
    814
    We do it by policy just looked it up and it is something like this:

    Policy: Delete user profiles older than a specified number of days on system restart

    Category Path: Computer Configuration\Administrative Templates\System\User Profiles

    This policy setting allows an administrator to automatically delete user profiles on system restart that have not been used within a specified number of days. Note: One day is interpreted as 24 hours after a specific user profile was accessed.

    If you enable this policy setting, the User profile Service will automatically delete on the next system restart all user profiles on the computer that have not been used within the specified number of days.

    TBH I am not sure this is exactly right but we definitely do it via GP

  3. #3
    chrbb's Avatar
    Join Date
    Oct 2005
    Location
    Midlands
    Posts
    1,509
    Thank Post
    141
    Thanked 67 Times in 62 Posts
    Rep Power
    47
    Yes I've seen that, but I like the script! Also I'd to be able to delete individual pofiles on an ad hoc basis much like using delprof in xp.

  4. #4

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,373
    Thank Post
    906
    Thanked 1,811 Times in 1,559 Posts
    Blog Entries
    12
    Rep Power
    468
    With Windows 7 you also need to remove something in the registery to manually delete profiles. Its pretty annouying to be honest.

    You do this by doing the following:


    To clear user local profile via registry :

    1.Press on Start > Run >Regedit

    2.Navigate to the following registry key :“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Window s NT\CurrentVersion\ProfileList”

    3.Under ProfileList navigate to binary key’s like this :S-1-5-21-3656904587-1668747452-4095529-500

    4.On the right side under ProfileImagePath you’’ll see the username and profile path.

    5.Chose the one with the desired user and delete the long reg key like :“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Window s NT\CurrentVersion\ProfileList”

    You can also look here on method one for a better idea

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



    Im not sure how to script it, im sure someone will though
    Last edited by FN-GM; 23rd March 2011 at 09:57 AM.

  5. 2 Thanks to FN-GM:

    chrbb (23rd March 2011), Griff (9th October 2013)

  6. #5

    bossman's Avatar
    Join Date
    Nov 2005
    Location
    England
    Posts
    4,035
    Thank Post
    1,262
    Thanked 1,107 Times in 785 Posts
    Rep Power
    338
    In Windows 7 if you don't want to go into the registry and delete the user profile keys a better way is to right click on Computer>properties>Advanced system settings>user profile settings>Highlight the required profile and then hit the delete button.

    This deletes the user profile and also the registry keys with it.

    If you just try to delete the user profile folder in C:\Users that user will not be able to log in to that workstation.

    I am sure that some script guy could knock something up to run through GPO or even at the users log off.

  7. #6
    chrbb's Avatar
    Join Date
    Oct 2005
    Location
    Midlands
    Posts
    1,509
    Thank Post
    141
    Thanked 67 Times in 62 Posts
    Rep Power
    47
    Tried both the Computer>properties>Advanced system settings>user profile settings>Highlight the required profile and then hit the delete button and the regedit version.
    Regedit was the winner!
    A profile that previously showed the pupil background suddenly had the dreaded windows 7 black background, deleted the profile using the regedit way and background back with the new profile.

  8. #7

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,373
    Thank Post
    906
    Thanked 1,811 Times in 1,559 Posts
    Blog Entries
    12
    Rep Power
    468
    Quote Originally Posted by chrbb View Post
    Tried both the Computer>properties>Advanced system settings>user profile settings>Highlight the required profile and then hit the delete button and the regedit version.
    Regedit was the winner!
    A profile that previously showed the pupil background suddenly had the dreaded windows 7 black background, deleted the profile using the regedit way and background back with the new profile.
    Glad it work, its abit of a pain really. Im hoping someone will have a script one day

  9. #8

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,159
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    125
    Quote Originally Posted by FN-GM View Post
    Glad it work, its abit of a pain really. Im hoping someone will have a script one day
    You should have asked :-) I'm guessing you want something like the one below (and I'm sure I've already posted this but I can't find it now!)

    The first chunk of this deletes the profiles that Windows knows about (ie the ones that show when you go to computer properties) - it does the registry clean up but sometimes fails to delete the folders on disc. The second part cleans up folders on disc which don't have a matching registry entry.

    You need to run this at machine startup - that way there won't be any odd files held open (Windows 7 is much better at closing registry handles but just occasionally something gets left open)

    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
    
    'get a list of profiles; exclude "special" (stuff like localservice) profiles
    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 (public etc)
    
    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"
    
    'look at each profile folder in turn
    set oFolder=ofso.getfolder(sRoot)
    for each oSubFolder in oFolder.subfolders
      sFolder=sRoot & lcase(oSubFolder.name)
      'check - is it in the list of "do not delete"
      if not(oDic.exists(sFolder)) then
        'orphaned folder so delete it
        ofso.deletefolder sFolder, true
      end if
    next

  10. 4 Thanks to srochford:

    chrbb (23rd March 2011), FN-GM (23rd March 2011), Marci (23rd March 2011), stevenlong1985 (25th April 2013)

  11. #9

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,373
    Thank Post
    906
    Thanked 1,811 Times in 1,559 Posts
    Blog Entries
    12
    Rep Power
    468
    Quote Originally Posted by srochford View Post
    You should have asked :-) I'm guessing you want something like the one below (and I'm sure I've already posted this but I can't find it now!)

    The first chunk of this deletes the profiles that Windows knows about (ie the ones that show when you go to computer properties) - it does the registry clean up but sometimes fails to delete the folders on disc. The second part cleans up folders on disc which don't have a matching registry entry.

    You need to run this at machine startup - that way there won't be any odd files held open (Windows 7 is much better at closing registry handles but just occasionally something gets left open)

    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
    
    'get a list of profiles; exclude "special" (stuff like localservice) profiles
    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 (public etc)
    
    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"
    
    'look at each profile folder in turn
    set oFolder=ofso.getfolder(sRoot)
    for each oSubFolder in oFolder.subfolders
      sFolder=sRoot & lcase(oSubFolder.name)
      'check - is it in the list of "do not delete"
      if not(oDic.exists(sFolder)) then
        'orphaned folder so delete it
        ofso.deletefolder sFolder, true
      end if
    next
    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

  12. #10

    Join Date
    Mar 2011
    Location
    Bournemouth
    Posts
    280
    Thank Post
    16
    Thanked 74 Times in 64 Posts
    Rep Power
    22
    Hi, I'm new to this site, but I thought I could contribute to this topic, since roaming profiles at my school have caused us quite a bit of agro.

    I wrote this script to purge profiles from a windows 7 machine, but after reading your thread I quickly added the option to purge a single profile since you said you sometimes do that.

    The script is vbs and takes 0, 1 or 2 arguments, the first argument is the name of the computer to purge profiles from, the second is a full or partial username string you can use to filter which profiles are deleted. If you dont specify a filter the script will delete all normal (ie not system, just normal user) profiles. If you dont specify a computer name it uses the local machine.

    The script uses the Win32_UserProfile WMI class to delete user profiles and also removes the almost-empty folders that are sometimes left behind to prevent windows suffixing the domain name on a new folder and to keep things tidy. It's only been tested on windows 7 but will probably work on vista as well. Needless to say, it requires administrator privileges and does not interfere with profiles which are current in use.

    I eventually replaced this script with a .net application that uses information from AD to wipe specific profiles from all our machines automatically. It uses a client service and a database to delete profiles we ask even if the machines are turned off and to make sure it doesn't remove profiles created after we triggered it. This would be difficult to post though. Nevertheless I don't use this script any more else I'd rewrite it in powershell, but it works and I hope it helps you.

    Edit: Looks like someone beat me to it, but will leave it here anyway :)

    Code:
    On Error Resume Next
    
    ' ====================================
    ' Begin Force CSCRIPT Execution Engine
    ' ====================================
    Dim objShell, strEngine, strArgs, strCmd, i
    Set objShell = CreateObject("WScript.Shell")
    strEngine = UCase(Right(WScript.FullName, 12))
    
    If strEngine <> "\CSCRIPT.EXE" Then
        ' Recreate the list of command line arguments
        strArgs = ""
        If WScript.Arguments.Count > 0 Then
            For i = 0 To WScript.Arguments.Count
                strArgs = strArgs & " " & WScript.Arguments(i)
            Next
        End If
    
        ' Create the complete command line to rerun this script in CSCRIPT
        strCmd = "cscript.exe //nologo """ & WScript.ScriptFullName & """" & strArgs
    
        objShell.Run strCmd, 1
    
        WScript.Quit
    End If
    ' ====================================
    ' End Force CSCRIPT Execution Engine
    ' ====================================
    
    WScript.Stdout.WriteLine
    WScript.Stdout.WriteLine "Avonbourne School Profile Deletion Script"
    WScript.Stdout.WriteLine "========================================="
    WScript.Stdout.WriteLine
    
    Dim objFSO, objWMIService, strComputer, strFilter, colProfiles, objProfile
    
    If WScript.Arguments.Count = 0 Then
    	strComputer = "."
    ElseIf WScript.Arguments.Count = 1 Then
    	strComputer = WScript.Arguments(0)
    	strFilter = "SID Like ""S-1-5-21%"" And Not LocalPath Like ""%Administrator%"""
    ElseIf WScript.Arguments.Count = 2 Then
    	strComputer = WScript.Arguments(0)
    	strFilter = "SID Like ""S-1-5-21%"" And LocalPath Like ""%" & WScript.Arguments(1) & "%"""
    Else
    	WScript.Stdout.WriteLine "Invalid command line parameters."
    	WScript.Quit
    End If
    
    WScript.Stdout.WriteLine "Deleting profiles from '" & strComputer & "'..."
    WScript.Stdout.WriteLine
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objWMIService = GetObject("Winmgmts:\\" & strComputer & "\root\cimv2") 
    Set colProfiles = objWMIService.ExecQuery("Select * From Win32_UserProfile Where " & strFilter)
    
    If Err.Number <> 0 Then
    	WScript.Stdout.WriteLine
    	WScript.Stdout.WriteLine "Error: " & Err.Number & ": " & Err.Description
    	WScript.Stdout.WriteLine
    	WScript.Exit
    End If
    
    If Not colProfiles Is Nothing Then
    	For Each objProfile in colProfiles
    		WScript.Stdout.WriteLine "Processing profile: Path: " & objProfile.LocalPath
    		WScript.Stdout.WriteLine "                    SID:  {" & objProfile.SID & "}"
    		
    		Err.Clear
    		
    		objProfile.Delete_
    		
    		If Err.Number = -2147024809 Then
    			WScript.Stdout.WriteLine "                    Profile in use, skipping."
    		ElseIf Err.Number = -2147024751 Then
    			objFSO.DeleteFolder objProfile.LocalPath, True
    			WScript.Stdout.WriteLine("                    Profile Deleted.")
    		ElseIf Err.Number <> 0 Then
    			WScript.Stdout.WriteLine " Failed!"
    			WScript.Stdout.WriteLine
    			WScript.Stdout.WriteLine "Error: " & Err.Number & ": " & Err.Description
    			WScript.Stdout.WriteLine
    		Else
    			WScript.Stdout.WriteLine("                    Profile Deleted.")
    		End If
    	Next
    End If

  13. 3 Thanks to ChrisMiles:

    chrbb (23rd March 2011), Marci (23rd March 2011), stevenlong1985 (25th April 2013)

  14. #11

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,159
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    125
    Quote Originally Posted by FN-GM View Post
    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.
    I'm using it on Windows 7 and it works fine. As far as I know, it works on Vista but I've not tried it (the WMI Win32_UserProfile stuff was introduced with Vista)

  15. #12

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,373
    Thank Post
    906
    Thanked 1,811 Times in 1,559 Posts
    Blog Entries
    12
    Rep Power
    468
    Quote Originally Posted by ChrisMiles View Post
    Hi, I'm new to this site, but I thought I could contribute to this topic, since roaming profiles at my school have caused us quite a bit of agro.

    I wrote this script to purge profiles from a windows 7 machine, but after reading your thread I quickly added the option to purge a single profile since you said you sometimes do that.

    The script is vbs and takes 0, 1 or 2 arguments, the first argument is the name of the computer to purge profiles from, the second is a full or partial username string you can use to filter which profiles are deleted. If you dont specify a filter the script will delete all normal (ie not system, just normal user) profiles. If you dont specify a computer name it uses the local machine.

    The script uses the Win32_UserProfile WMI class to delete user profiles and also removes the almost-empty folders that are sometimes left behind to prevent windows suffixing the domain name on a new folder and to keep things tidy. It's only been tested on windows 7 but will probably work on vista as well. Needless to say, it requires administrator privileges and does not interfere with profiles which are current in use.

    I eventually replaced this script with a .net application that uses information from AD to wipe specific profiles from all our machines automatically. It uses a client service and a database to delete profiles we ask even if the machines are turned off and to make sure it doesn't remove profiles created after we triggered it. This would be difficult to post though. Nevertheless I don't use this script any more else I'd rewrite it in powershell, but it works and I hope it helps you.

    Edit: Looks like someone beat me to it, but will leave it here anyway

    Code:
    On Error Resume Next
    
    ' ====================================
    ' Begin Force CSCRIPT Execution Engine
    ' ====================================
    Dim objShell, strEngine, strArgs, strCmd, i
    Set objShell = CreateObject("WScript.Shell")
    strEngine = UCase(Right(WScript.FullName, 12))
    
    If strEngine <> "\CSCRIPT.EXE" Then
        ' Recreate the list of command line arguments
        strArgs = ""
        If WScript.Arguments.Count > 0 Then
            For i = 0 To WScript.Arguments.Count
                strArgs = strArgs & " " & WScript.Arguments(i)
            Next
        End If
    
        ' Create the complete command line to rerun this script in CSCRIPT
        strCmd = "cscript.exe //nologo """ & WScript.ScriptFullName & """" & strArgs
    
        objShell.Run strCmd, 1
    
        WScript.Quit
    End If
    ' ====================================
    ' End Force CSCRIPT Execution Engine
    ' ====================================
    
    WScript.Stdout.WriteLine
    WScript.Stdout.WriteLine "Avonbourne School Profile Deletion Script"
    WScript.Stdout.WriteLine "========================================="
    WScript.Stdout.WriteLine
    
    Dim objFSO, objWMIService, strComputer, strFilter, colProfiles, objProfile
    
    If WScript.Arguments.Count = 0 Then
    	strComputer = "."
    ElseIf WScript.Arguments.Count = 1 Then
    	strComputer = WScript.Arguments(0)
    	strFilter = "SID Like ""S-1-5-21%"" And Not LocalPath Like ""%Administrator%"""
    ElseIf WScript.Arguments.Count = 2 Then
    	strComputer = WScript.Arguments(0)
    	strFilter = "SID Like ""S-1-5-21%"" And LocalPath Like ""%" & WScript.Arguments(1) & "%"""
    Else
    	WScript.Stdout.WriteLine "Invalid command line parameters."
    	WScript.Quit
    End If
    
    WScript.Stdout.WriteLine "Deleting profiles from '" & strComputer & "'..."
    WScript.Stdout.WriteLine
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objWMIService = GetObject("Winmgmts:\\" & strComputer & "\root\cimv2") 
    Set colProfiles = objWMIService.ExecQuery("Select * From Win32_UserProfile Where " & strFilter)
    
    If Err.Number <> 0 Then
    	WScript.Stdout.WriteLine
    	WScript.Stdout.WriteLine "Error: " & Err.Number & ": " & Err.Description
    	WScript.Stdout.WriteLine
    	WScript.Exit
    End If
    
    If Not colProfiles Is Nothing Then
    	For Each objProfile in colProfiles
    		WScript.Stdout.WriteLine "Processing profile: Path: " & objProfile.LocalPath
    		WScript.Stdout.WriteLine "                    SID:  {" & objProfile.SID & "}"
    		
    		Err.Clear
    		
    		objProfile.Delete_
    		
    		If Err.Number = -2147024809 Then
    			WScript.Stdout.WriteLine "                    Profile in use, skipping."
    		ElseIf Err.Number = -2147024751 Then
    			objFSO.DeleteFolder objProfile.LocalPath, True
    			WScript.Stdout.WriteLine("                    Profile Deleted.")
    		ElseIf Err.Number <> 0 Then
    			WScript.Stdout.WriteLine " Failed!"
    			WScript.Stdout.WriteLine
    			WScript.Stdout.WriteLine "Error: " & Err.Number & ": " & Err.Description
    			WScript.Stdout.WriteLine
    		Else
    			WScript.Stdout.WriteLine("                    Profile Deleted.")
    		End If
    	Next
    End If
    Thanks for posting. I will give it a shot.

    Is this just ready to go as a startup script?

  16. #13

    Join Date
    Mar 2011
    Location
    Bournemouth
    Posts
    280
    Thank Post
    16
    Thanked 74 Times in 64 Posts
    Rep Power
    22
    Its not really geared for a startup script, theres loads of output in there which can be stripped out. This would work better if all you want is to trash all user profiles in the machine logon script. Youll also need to ensure that this runs directly via cscript in the machine startup batch. If you let it run in wscript the computer wont wait for it to finish and users might be able to log in while its still running:

    Code:
    Dim objFSO, objWMIService, colProfiles, objProfile
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objWMIService = GetObject("Winmgmts:\\.\root\cimv2") 
    Set colProfiles = objWMIService.ExecQuery("Select * From Win32_UserProfile Where SID Like ""S-1-5-21%"" And Not LocalPath Like ""%Administrator%""")
    
    If Err.Number <> 0 Then
    	WScript.Exit
    End If
    
    If Not colProfiles Is Nothing Then
    	For Each objProfile in colProfiles
    		Err.Clear
    		
    		objProfile.Delete_
    		
    		If Err.Number = -2147024751 Then
    			objFSO.DeleteFolder objProfile.LocalPath, True
    		End If
    	Next
    End If

  17. #14

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,373
    Thank Post
    906
    Thanked 1,811 Times in 1,559 Posts
    Blog Entries
    12
    Rep Power
    468
    Quote Originally Posted by srochford View Post
    You should have asked :-) I'm guessing you want something like the one below (and I'm sure I've already posted this but I can't find it now!)

    The first chunk of this deletes the profiles that Windows knows about (ie the ones that show when you go to computer properties) - it does the registry clean up but sometimes fails to delete the folders on disc. The second part cleans up folders on disc which don't have a matching registry entry.

    You need to run this at machine startup - that way there won't be any odd files held open (Windows 7 is much better at closing registry handles but just occasionally something gets left open)

    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
    
    'get a list of profiles; exclude "special" (stuff like localservice) profiles
    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 (public etc)
    
    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"
    
    'look at each profile folder in turn
    set oFolder=ofso.getfolder(sRoot)
    for each oSubFolder in oFolder.subfolders
      sFolder=sRoot & lcase(oSubFolder.name)
      'check - is it in the list of "do not delete"
      if not(oDic.exists(sFolder)) then
        'orphaned folder so delete it
        ofso.deletefolder sFolder, true
      end if
    next

    Hi

    I am getting the attached message. This is running it on a Server 2008 Terminal Server. Any ideas please?
    Attached Images Attached Images

  18. #15

    Join Date
    Mar 2011
    Location
    Bournemouth
    Posts
    280
    Thank Post
    16
    Thanked 74 Times in 64 Posts
    Rep Power
    22
    Quote Originally Posted by FN-GM View Post
    Hi

    I am getting the attached message. This is running it on a Server 2008 Terminal Server. Any ideas please?
    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



SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Deleting profiles
    By GoldenWonder in forum Windows 7
    Replies: 9
    Last Post: 11th February 2010, 11:04 AM
  2. Deleting Local Profiles
    By Mr_M_Cox in forum Windows
    Replies: 43
    Last Post: 17th March 2009, 03:02 PM
  3. deleting profiles
    By Millsy79 in forum Windows
    Replies: 7
    Last Post: 31st July 2008, 08:40 PM
  4. Deleting profiles on server
    By pinemarten in forum General Chat
    Replies: 4
    Last Post: 29th August 2006, 09:44 PM
  5. Deleting User Profiles
    By Gordie in forum Scripts
    Replies: 4
    Last Post: 19th June 2006, 04: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
  •