+ Post New Thread
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18
Windows 7 Thread, GPO Windows 7 - Automatically Delete Local User Profiles Older Than X number of Days in Technical; Originally Posted by srochford We run this script every night to clean up. The check at the start is to ...
  1. #16

    Join Date
    Apr 2011
    Location
    Edinburgh
    Posts
    2
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Help

    Quote Originally Posted by srochford View Post
    We run this script every night to clean up. The check at the start is to exclude certain machines (the ones in lecture theatres)

    The next section uses a WMI call to get a list of profiles other than the "special" ones (localsystem etc) and delete them (this is what you see when you go to the control panel and delete profiles)

    This will sometimes leave bits behind so the next step is to get a list of folders which shouldn't be deleted by reading the existing profiles from the registry - this list is built in a dictionary. To this is then added things like "public" and "default".

    The script then scans c:\users and checks each folder it finds against the dictionary. If the folder isn't listed then it gets deleted (because it doesn't need to be there)

    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"
    
    
    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
    Hi There

    I was wanting to use this script but cannot get it to work in Windows 7, nothing seems to happen.

    I was hoping you could advise further

    Thanks

  2. IDG Tech News

  3. #17

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    1,425
    Thank Post
    175
    Thanked 226 Times in 212 Posts
    Rep Power
    113
    Would help if you could expand a bit :P

    What's not working? What users aren't being deleted? Do you have the AV folder that breaks out of the script?

    etc etc

    Steve

  4. #18

    Join Date
    Apr 2011
    Location
    Edinburgh
    Posts
    2
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Hi

    Quote Originally Posted by Steve21 View Post
    Would help if you could expand a bit :P

    What's not working? What users aren't being deleted? Do you have the AV folder that breaks out of the script?

    etc etc

    Steve
    I removed the AV section as it didn't apply, and i have run the script manually and it seems to be doing something for a couple of minutes but all the profiles are still there, even in the registry.

    any ideas

SHARE:
+ Post New Thread
Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 28
    Last Post: 23rd May 2011, 09:57 AM
  2. Delete local profiles.
    By firefighting in forum How do you do....it?
    Replies: 8
    Last Post: 28th April 2011, 08:28 PM
  3. Replies: 2
    Last Post: 13th January 2011, 01:28 PM
  4. Delete User profiles on local machines
    By martinb in forum How do you do....it?
    Replies: 2
    Last Post: 16th March 2010, 11:13 AM
  5. Replies: 6
    Last Post: 26th September 2008, 03:57 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •