How can I get the logged in user (not A_Username)?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
knn
Posts: 30
Joined: 30 Mar 2020, 20:35

How can I get the logged in user (not A_Username)?

22 Apr 2020, 15:12

Is there any way to find out the logged in user? Meaning, the user that is logged in the current windows session.

%A_Username% contains only the user who's running the autohotkey.exe process, so when I right click and then "Run as administrator" it contains the administrator's username, not the username of the user who has logged into windows.
User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: How can I get the logged in user (not A_Username)?

22 Apr 2020, 16:19

See if this provides the username you're looking for:

Code: Select all

RegExMatch(A_AppData, "\\users\\\K[^\\]+", Username)
MsgBox, % Username
knn
Posts: 30
Joined: 30 Mar 2020, 20:35

Re: How can I get the logged in user (not A_Username)?

22 Apr 2020, 17:34

Nope, it shows the admin's username, too.
User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: How can I get the logged in user (not A_Username)?

22 Apr 2020, 18:55

How about this?

Code: Select all

Username := Username()
MsgBox, % Username
return

Username() {
    out := ComObjCreate("WScript.Shell").Exec(ComSpec " /C whoami").StdOut.ReadAll()
	return RegexReplace(out, "[^\\]+\\",, 1)
}
knn
Posts: 30
Joined: 30 Mar 2020, 20:35

Re: How can I get the logged in user (not A_Username)?

23 Apr 2020, 18:37

No, that doesn't work either. I even tried to run it with reduced (= anti-elevated) credentials via

Code: Select all

runas /trustlevel:0x20000 [batch file with whoami]
and it still shows the administrator's name.

So as it stands, non-elevated AHK doesn't work (because it's non-functional in elevated programs) and elevated AHK also doesn't work (because it doesn't know the logged in username).
knn
Posts: 30
Joined: 30 Mar 2020, 20:35

Re: How can I get the logged in user (not A_Username)?

24 Apr 2020, 04:58

Thank you for this impressive code. That gives me an array of all logged in users, how do I know which of them is the one who pressed a button?
knn
Posts: 30
Joined: 30 Mar 2020, 20:35

Re: How can I get the logged in user (not A_Username)?

25 Apr 2020, 09:12

OK, I think I found the necessary code based on wmic computersystem get username.

Please tell me if it works for you and whether there can be some optimizations made.

Code: Select all

wmicOut := ComObjCreate("WScript.Shell").Exec(ComSpec " /C wmic computersystem get username").StdOut.ReadAll()
wmicOut := RegexReplace(wmicOut, "[`r`n]+", "`n")
wmicOutAR := StrSplit(wmicOut, "`n", " `t")
domainusername := wmicOutAR[2]
finalusername := trim(RegexReplace(domainusername, "[^\\]+\\",, 1))
MsgBox % "-->" . finalusername . "<--"
User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: How can I get the logged in user (not A_Username)?

25 Apr 2020, 09:30

You can reduce the output to just the username with one RegEx statement.

Code: Select all

wmicOut := ComObjCreate("WScript.Shell").Exec(ComSpec " /C wmic computersystem get username").StdOut.ReadAll()
RegExMatch(wmicOut, "(?<=\\)\w+", username)
MsgBox % "-->" . username . "<--"
knn
Posts: 30
Joined: 30 Mar 2020, 20:35

Re: How can I get the logged in user (not A_Username)?

25 Apr 2020, 15:15

OK, got this now since the username can contain not only alphanumeric chars

Code: Select all

wmicOut := ComObjCreate("WScript.Shell").Exec(ComSpec " /C wmic computersystem get username").StdOut.ReadAll()
RegExMatch(wmicOut, "(?<=\\).*?(?=$|`n|`r)", username)
username := trim(username)
MsgBox % "-->" . username . "<--"

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mstrauss2021 and 303 guests