Solved

Deleting Old Profiles

Posted on 2011-09-13
2
360 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
Comment Utility
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
Comment Utility
That was fast and accurate! Thanks a ton!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

762 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

11 Experts available now in Live!

Get 1:1 Help Now