Downloads
21569.zip

Do you have a scripting-related question or problem? You can send your question or problem to winscriptsol@win2000mag.com.

Using VBScript, how can I obtain the username of the currently logged-on user?

You have several options. You'll need to decide which option is the best solution for you.

The easiest way to obtain the username of the currently logged-on user (i.e., the current username) is to read the value in the %USERNAME% environment variable. Listing 1, page 6, demonstrates how to use VBScript code in the Windows Script Host (WSH) environment to read this value.

You can also obtain the value of %USERNAME% directly from the command line or a batch file. You simply use the command processor's built-in Echo command:

C:\> Echo %USERNAME%

WSH provides a mechanism to obtain the current username. As Listing 2 shows, you read the value of the WSH Network object's Username property.

If you need to obtain the current username in a format that you can subsequently use to bind to the corresponding User object, you can use one of the two following Microsoft Active Directory Service Interfaces (ADSI) utility interfaces: IADsWinNTSystemInfo or IADsADSystemInfo.

You use IADsWinNTSystemInfo when the user account resides in a registry-based SAM, which is a Windows NT 4.0 domain SAM, NT 4.0 member server SAM, NT Workstation 4.0 SAM, Windows 2000 Server non-domain controller (non-DC) SAM, or Win2K Professional SAM. You use IADsADSystemInfo when the user account resides in Win2K's Active Directory (AD). Listing 3 and Listing 4 contain code that demonstrates how to use these two ADSI utility interfaces to obtain the current username and use the returned username to bind to the corresponding User object in the underlying directory.

To use the IADsWinNTSystemInfo and IADsADSystemInfo utility interfaces on NT 4.0 clients, you must install the Active Directory Client Extensions for NT Workstation 4.0. To learn about and download these extensions, go to http://www.microsoft.com/windows2000/server/evaluation/news/bulletins/adextension.asp. Win2K Pro and Win2K Server include the two ADSI utility interfaces by default.