Interactive script to delete user Win 7 profiles and specific folder in user my documents on server

Posted on 2012-09-02
Last Modified: 2013-01-11
We have the requirement for a batch file / vbscript that can be run interactively - essentially what I would like is a prompt for a username, and the script deletes all the files and folders inside the stored v2 profile on a Windows DC as well as deletes all the files and folders inside a specifc folder we use (and is the same for every user- apart from server name and username) for roaming Windows 7 settings. Some logging would be great as well - i.e. a record of who ran what at what time - (what user was deleted etc.)

We have 5 DCs, with our user profiles and work areas stored across all of them. All our users have a hidden share - \\servername\username$ which is their work area. Inside this folder are the Windows 7 roaming settings ( a couple of folders beneath the root username$ folder) The v2 profiles are stored in a folder path - \\servername\UserProfiles\YearGroup\<username>.v2

If you require any other information - I will provide it.

The reason for wanting this is we need a quick method to delete user v2 profiles as well as removing windows 7 settings to assist troubleshooting our new Windows 7 machines on the domain.

Thank you.
Question by:and235100
    LVL 65

    Accepted Solution

    Hi there Andrew,

    As a start, give this a whirl, and see if the paths match up to what you need to delete, when you view the log file.

    I have commented out both

    line so that it won't delete anything.



    strOutput = "output.csv"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Const ForAppending = 8
    Set objNetwork = CreateObject("WScript.Network")
    arrServers = Array( _
    	"DCServer1", _
    	"DCServer2", _
    	"DCServer3", _
    	"DCServer4", _
    	"DCServer5" _
    strUsername = Trim(InputBox("Enter a username to delete settings for:", "Username"))
    If strUsername <> "" Then
    	strRoamingFolder = "<NOT FOUND>"
    	strV2Profile = "<NOT FOUND>"
    	For Each strServer In arrServers
    		If objFSO.FolderExists("\\" & strServer & "\" & strUsername & "$\Application Data\Roaming\Microsoft") = True Then
    			strRoamingFolder = "\\" & strServer & "\" & strUsername & "$\Application Data\Roaming\Microsoft"
    		End If
    		If objFSO.FolderExists("\\" & strServer & "\UserProfiles\YearGroup\" & strUsername & ".v2") = True Then
    			strV2Profile = "\\" & strServer & "\UserProfiles\YearGroup\" & strUsername & ".v2"
    		End If
    	If strRoamingFolder <> "<NOT FOUND>" Or strV2Profile <> "<NOT FOUND>" Then
    		If objFSO.FileExists(strOutput) = False Then
    			Set objOutput = objFSO.OpenTextFile(strOutput, True)
    			objOutput.WriteLine """Time"",""Iniated By"",""User Specified"",""User Roaming Folder"",""V2 Profile"""
    			Set objOutput = objFSO.OpenTextFile(strOutput, ForAppending, False)
    		End If
    		If objFSO.FolderExists(strRoamingFolder) = True Then
    			'objFSO.DeleteFile strRoamingFolder & "\*"
    		End If
    		If objFSO.FolderExists(strV2Profile) = True Then
    			'objFSO.DeleteFile strV2Profile & "\*"
    		End If		
    		objOutput.WriteLine """" & Now & """,""" & objNetwork.Username & """,""" & strUsername & """,""" & strRoamingFolder & """,""" & strV2Profile & """"
    		WScript.Echo "Shared folder and V2 Profile folder for " & strUsername & " were not found on any server."
    	End If
    	WScript.Echo "No user specified."
    End If
    WScript.Echo "Done. Please see " & strOutput

    Open in new window

    LVL 32

    Author Comment

    I didn't have time to check this. Thanks anyway.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
    Learn about cloud computing and its benefits for small business owners.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    746 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

    15 Experts available now in Live!

    Get 1:1 Help Now