Stanislav Stoyanov
Stanislav Stoyanov

Reputation: 2120

How to get or retrieve different users´ desktop folder than current user´s desktop folder with VBScript?

On a Windows computer there are three users: Administrator, UserA and UserB

How to retrieve the path to UserA´s desktop folder and UserB respectively when I am logged in as Administrator?

I am looking for VBScript that will run on Windows 7 and Windows Server 2008.

I tried SpecialFolders("Desktop"), but this returns the Administrator´s desktop folder path, not the desktop folder path of UserA.

As a result I want to get the paths of C:\Users\UserA\Desktop and C:\Users\UserB\Desktop.

Also looking a way to retrieve folder Start Menu\Programs of UserA and UserB.

Upvotes: 3

Views: 6943

Answers (2)

Nilpo
Nilpo

Reputation: 4816

Why is everyone so quick to say this cannot be done? This is very easy to do with WMI.

I've broken the script into pieces so you can see how I'm performing each step.

arrAccounts = Array("UserA", "UserB")

For Each strUser in arrAccounts
    WScript.Echo GetUserDesktop(GetSID(strUser))
Next

Function GetUserDesktop(strSID)
    Const HKEY_USERS = &H80000003

    strComputer = "."
    Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\default:StdRegProv")
    strKeyPath = strSID & "\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
    strValueName = "Desktop"
    objRegistry.GetStringValue HKEY_USERS, strKeyPath, strValueName, strValue
    GetUserDesktop = strValue
End Function

Function GetSID(strUser)
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    ' Get the computer name (using WMI)
    For Each objComputer In objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
        strComputerName = objComputer.Name
        Exit For
    Next

    ' You could just as easily do this...
'   strComputerName = CreateObject("WScript.Network").ComputerName

    Set objAccount = objWMIService.Get _
        ("Win32_UserAccount.Name='" & strUser & "',Domain='" & strComputerName & "'")
    GetSID = objAccount.SID
End Function

Just be aware that Microsoft advises against using the registry to determine the location of user shell folders. There is no guarantee that this method will continue to work in future versions of Windows, but it does work in the ones you intend to target so why not use it?

Upvotes: 2

Helen
Helen

Reputation: 97707

(Adapted from my answer on a similar question Getting special Folder path for a given user in Jscript.)

This can't be done in pure VBcript and Windows Script Host, not without external utilities.

On the other hand, if you were to use a language supporting Windows API calls (such as C# or C++), you would be able to perform your task either a) using the SHGetKnownFolderPath function (or SHGetFolderPath on Windows versions prior to Vista), or b) by reading the path from that user's registry hive. See these questions for details:

Upvotes: 1

Related Questions