+ Post New Thread
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19
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. #17

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,698
    Thank Post
    335
    Thanked 516 Times in 484 Posts
    Rep Power
    180
    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

  3. #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

  4. #19

    Join Date
    Aug 2012
    Location
    Pinedale
    Posts
    1
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Yes it works.

    It's been a while since you posted this question. Sorry for the slow response.

    The GPO to delete user profiles works, all too well I'm afraid. We set this up for precisely the reason you state and because we have some terminal servers we didn't want to have hundreds and hundreds of user profiles taking space and slowing the server. So I set this to 30 days. Until August everything was just great and we had forgotten about even making the change. Now as teachers return they are missing a bunch of stuff they saved because it was on their laptops and/or computers instead of their network drive.

    We have been able to use Recuva to retrieve the deleted files.

    This also works well for viruses that delete or move the contents of the desktop.

    Now we set the X number of days to 180...

    Chris



    Quote Originally Posted by nsuljic View Post
    Hello Everyone - I know that some of you probably use scripts to delete old profiles, but I wanted to see if anyone has been able to get the Group Policy working that is supposed to delete user profiles older than X number of days?

    Computer Configuration
    -Policies
    --Administrative Templates
    ---System/User Profiles
    ----"Delete user profiles older than a specified number of days on system restart"

    I run a couple of computer labs at a Medical School and we have a lot of students logging in to the machines. Within a short period of time, a lot of local profiles build up and degrade the performance of the computers. Any suggestions would be much appreciated.

    Thanks,
    Nermin

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, 12: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, 10:13 AM
  5. Replies: 6
    Last Post: 26th September 2008, 03:57 PM

Thread Information

Users Browsing this Thread

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

  1. MattElms

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
  •