I was recreating a login script for a client that mapped a drive to the location of their redirected My Documents. Because they were in the middle of a migration the My Documents could be in a different spot depending on if a user had been migrated to a new environment or not. So I needed to be able to query the current location of the My Docs, much like the %userprofile% shows the path to the current profile.
Below is a chunk of VBScript I cobbled together from a variety of sources including ActiveXperts that reads the appropriate registry key value of the user that runs the script. This chunk just outputs the path to the command line but the myDocsPath variable could also be used to map a drive.
(Please note that the line that starts “Set oReg=GetObject….” and the line below it need to be a single line)
—-Copy Everything Below—–
const HKEY_CURRENT_USER = &H80000001 const HKEY_LOCAL_MACHINE = &H80000002 strComputer = "." Set StdOut = WScript.StdOut Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv") strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" strValueName = "Personal" oReg.GetExpandedStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,myDocsPath StdOut.WriteLine "Current My Docs path is: " & myDocsPath
—-Copy Everything Above—–