Solved

Deleting Old Profiles

Posted on 2011-09-13
2
362 Views
Last Modified: 2012-05-12
I have peiced together some script from various resources to try to accomplish this goal:

I want a script that will delete all local user profiles over 6 months old, except ones with the names:
            "Administrator"
            "All Users"
            "nvconfig"
            "OtherExamples"

Here is the Code I currently have, please help me figure out what I am doing wrong.

'=================================================================

Const LocalDocumentsFolder = "C:\Documents and Settings\"

set objFSO = createobject("Scripting.FileSystemObject")
set objFolder = objFSO.GetFolder(localdocumentsfolder)
set profolder = objFolder.SubFolders

on error resume next

for each fldr in profolder
      if not isexception(fldr.name) AND DateDiff("D", profolder.DateLastModified, Now) < 180 Then
                  'objFSO.DeleteFolder fldr.path, True
                  wscript.echo "Deleted Folder: " & profolder
      end if
next

Function isException(byval foldername)
      select case foldername
            case "All Users"
                  isException = True
            case "Default User"
                  isException = True
            case "nvconfig"
                  isException = True
            case "admin"
                  isException = True
            case "Administrator"
                  isException = True
            case Else
                  isException = False
      End Select
End Function
'==========================================================


0
Comment
Question by:stayingdusty
2 Comments
 
LVL 14

Accepted Solution

by:
sungenwang earned 500 total points
ID: 36530945
This below should work. When troubleshooting, you should remove "on error resume next" so you know which line is having issues.

The issue was this line:
      if not isexception(fldr.name) AND DateDiff("d", fldr.DateLastModified, Now) > 180 Then

where you should use fldr.DateLastModified instead of profolder.DateLastModified

sew

'=================================================================

Const LocalDocumentsFolder = "C:\Documents and Settings\"

set objFSO = createobject("Scripting.FileSystemObject")
set objFolder = objFSO.GetFolder(localdocumentsfolder)
set profolder = objFolder.SubFolders

on error resume next

for each fldr in profolder
      if not isexception(fldr.name) AND DateDiff("d", fldr.DateLastModified, Now) > 180 Then
                  'objFSO.DeleteFolder fldr.path, True
                  wscript.echo "Deleted Folder: " & fldr.path
      end if
next

Function isException(byval foldername)
      select case foldername
            case "All Users"
                  isException = True
            case "Default User"
                  isException = True
            case "nvconfig"
                  isException = True
            case "admin"
                  isException = True
            case "Administrator"
                  isException = True
            case Else
                  isException = False
      End Select
End Function
'==========================================================

Open in new window

0
 

Author Closing Comment

by:stayingdusty
ID: 36531178
That was fast and accurate! Thanks a ton!
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio search word table and return Cell index 8 48
how to copy from computer to usb drive 7 109
T-SQL:  Sigh---Boy, this is fun.... 12 32
VBScript Issues 8 14
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now