[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2012-09-02
Medium Priority
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

RobSampson earned 1500 total points
ID: 38397452
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

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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Learn about cloud computing and its benefits for small business owners.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

834 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