List All Folders in the User Profile Directory

I wanted to be able to remotely connect to a server and see if it had a profile directory for a particular user.  I wrote a VBScript that will connect to a machine through WMI, find out what the ProfilesDirectory path is and then list all the folders in that directory.  It wouldn’t take much to be able to output to a text file or some other format.

The next thing I will work on is to have it pull a list of machines from somewhere, go check those machines and then when it finds a particular profile on that machine to go and remove it.   You can see what I have so far:

Const HKLM = &H80000002

Dim strComputer

strComputer = “.” ‘This computer is the “.”.  If you want another computer, replace the .
Set oReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\\” & _
    strComputer & “\root\default:StdRegProv”)
strKeyPath = “SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\”
strValueName = “ProfilesDirectory”

oReg.GetExpandedStringValue HKLM,strKeyPath,strValueName,strValue

If strValue = “%SystemDrive%\Documents and Settings” Then
    WScript.Echo “Default Path”
    strProfilePath = “C:\Documents and Settings”
Else
    WScript.Echo strValue
    strProfilePath = strValue
End If

Set objWMIService = GetObject(“winmgmts:” _
 & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colSubfolders = objWMIService.ExecQuery _
 (“ASSOCIATORS OF {Win32_Directory.Name='” & strProfilePath & “‘} ” _
 & “WHERE AssocClass = Win32_Subdirectory ” _
 & “ResultRole = PartComponent”)
For Each objFolder in colSubfolders
 Wscript.Echo objFolder.Name
Next

Leave a Reply