How to query the current logged in user using VBScript when running VBS under System account

Posted on 2014-08-26
Last Modified: 2014-09-02
We use SCCM to push out applications and updates to users. Regular staff are denied access to install any files as well as edit the registry. In SCCM, we have installs run under the System account which grants the accesses it needs to install programs. In my current case, I have a Batch file that does 2 things.. 1. install the application. 2. run a VBS that pulls the UserName and then creates a REG file that writes a bunch of lines as well as takes the username (objNetwork.UserName) and inputs it into one of the lines:
a.WriteLine("""HostName""=""IPC-" & objNetwork.UserName & """")

Open in new window

Then imports it into the Registry.

Here is the problem...because this all gets done under the System account in SCCM, the VBS thinks that the username is "System" so inputs it as system when it really should have been the logged in users name. I need to find a way to get the VBS to pull the actual user logged in. I was thinking that it can query the last logged in user which would obviously be the current user but I do not know how to code this. Any one have any ideas as to how I can make this work?

The actual code I am using is referenced in this previous post so I would be adding it to the code that I posted near the bottom:
Question by:prologic08

    Accepted Solution

    I'm going to try this:

    Option Explicit
    Dim WSHShell, RegKey, Username
    Set WSHShell = CreateObject("WScript.Shell")
    RegKey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\"
    Username = WSHShell.RegRead(RegKey & "LastLoggedOnSAMUser")
    Username = Mid(Username,7) 'I have my domain inside of this value so I used this to remove the first 6 characters
    WScript.echo Username

    Open in new window

    LVL 44

    Expert Comment

    What does the environment variable hold?  If it is the actual user and not system, you should be able to use something like this:
    Set oWshShell = wscript.CreateObject( “” )
    strUserName = oWshShell.ExpandEnvironmentStrings( “%USERNAME%” )

    Open in new window

    LVL 65

    Assisted Solution

    You can also see what this WMI code gives you.



    strComputer = "."
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
    Set colComputer = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
    For Each objComputer in colComputer
    	strUserName = objComputer.UserName
    WScript.Echo strUserName

    Open in new window


    Author Comment

    On my machine the variable shows as SYSTEM.. My last comment above seemed to do the trick anyway so I would say I am good. Thanks for responding.

    Author Comment

    RobSampson, That gives me the same output as the code I posted so I can use either one.

    Author Closing Comment

    Since I was able to find my own answer before Rob posted his, I am splitting it between the both of us since either will work.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now