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.
How can I get the logged in user (not A_Username)?
Re: How can I get the logged in user (not A_Username)?
See if this provides the username you're looking for:
Code: Select all
RegExMatch(A_AppData, "\\users\\\K[^\\]+", Username)
MsgBox, % Username
Re: How can I get the logged in user (not A_Username)?
Nope, it shows the admin's username, too.
Re: How can I get the logged in user (not A_Username)?
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)
}
Re: How can I get the logged in user (not A_Username)?
No, that doesn't work either. I even tried to run it with reduced (= anti-elevated) credentials via
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).
Code: Select all
runas /trustlevel:0x20000 [batch file with whoami]
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).
Re: How can I get the logged in user (not A_Username)?
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Re: How can I get the logged in user (not A_Username)?
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?jNizM wrote: Try this -> https://www.autohotkey.com/boards/viewtopic.php?p=305926#p305926
Re: How can I get the logged in user (not A_Username)?
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.
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 . "<--"
Re: How can I get the logged in user (not A_Username)?
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 . "<--"
Re: How can I get the logged in user (not A_Username)?
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 . "<--"
Who is online
Users browsing this forum: mstrauss2021 and 303 guests