How to get logged in user name?

There are two methods I know of to obtain the currently logged in username.  Both fail when I try to use them in a script that is running under a different user account.

Specifically, I have a script that is run from a Scheduled Task.  The principal for the Task is a local user account that has 'run as batch job' permissions.  Domain users are logged in when the script is launched by Task Scheduler.

I need the script to obtain the username of the currently logged in domain user.  How can I do this when the script is running under a different (local) user account?

Method 1:

$array = @(Get-WmiObject -ComputerName . -Namespace root\cimv2 -Class Win32_ComputerSystem | foreach { $_.username }).Split('\\') $username = "$(($array[0]).ToUpper())\$(($array[1]).ToLower())" RESULT: MYDOMAIN\myuser.name

(works only when script is run by myuser.name)

Method 2:

$username = ($env:USERNAME).ToLower()

RESULT:

myuser.name

(again, works only when script is run by myuser.name)

July 22nd, 2015 7:34pm

This method always gives you the user logged in at the console no matter who runs it:

Get-WmiObject Win32_ComputerSystem | select username

The rest of the code you posted is not good code for almost anything.

$env:USERNAME always gives the current user.  The one that is running the script.

On a terminal server (RDS) this doesn't work at all.Start here to learn how PowerShell works: https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx?f=255&MSPPError=-2147217396

There isn't really any place to just learn WMI but there are a number of blogs that can help you learn WMI.  For learning how Windows works I suggest looking for one of the books called "Windows Internals"

Free Windows Admin Tool Kit Click here and download it now
July 22nd, 2015 8:50pm

I have the book "PowerShell and WMI" by Richard Siddaway I would recommend

http://www.amazon.com/PowerShell-WMI-Richard-Siddaway/dp/1617290114/ref=sr_1_1?s=books&ie=UTF8&qid=1437621574&sr=1-1&keywords=PowerShell+and+WMI

July 22nd, 2015 11:30pm

Try this.
gwmi win32_computersystem|select USername

It will return logged on username.
Free Windows Admin Tool Kit Click here and download it now
July 23rd, 2015 12:26am

I have the book "PowerShell and WMI" by Richard Siddaway I would recommend

http://www.amazon.com/PowerShell-WMI-Richard-Siddaway/dp/1617290114/ref=sr_1_1?s=books&ie=UTF8&qid=1437621574&sr=1-1&keywords=PowerShell+and+WMI

  • Marked as answer by jott220 14 hours 10 minutes ago
July 23rd, 2015 3:21am

I have the book "PowerShell and WMI" by Richard Siddaway I would recommend

http://www.amazon.com/PowerShell-WMI-Richard-Siddaway/dp/1617290114/ref=sr_1_1?s=books&ie=UTF8&qid=1437621574&sr=1-1&keywords=PowerShell+and+WMI

  • Marked as answer by jott220 Thursday, July 23, 2015 4:59 PM
Free Windows Admin Tool Kit Click here and download it now
July 23rd, 2015 3:21am

Thanks Larry. Looks like a helpful resource.

I think the answer I was looking for is this:

$username = (Get-WmiObject Win32_ComputerSystem | 
    Select-Object -ExpandProperty UserName).Split('\')[1]

  • Edited by jott220 15 hours 9 minutes ago
July 23rd, 2015 12:10pm

Not what your original post says:

(Get-WmiObject Win32_ComputerSystem).Username.Split('\')[-1]

Your OP is this:
$username = "$(($array[0]).ToUpper())\$(($array[1]).ToLower())"

Which is unnecessary as you are just converting back to what you started with.

Free Windows Admin Tool Kit Click here and download it now
July 23rd, 2015 12:35pm

Thanks Larry. Looks like a helpful resource.

I think the answer I was looking for is this:

$username = (Get-WmiObject Win32_ComputerSystem | 
    Select-Object -ExpandProperty UserName).Split('\')[1]

  • Edited by jott220 Thursday, July 23, 2015 4:00 PM
July 23rd, 2015 4:00pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics