Getting Local Logged In User

I'm working on a project where I need to get a friendly name for the local user logged in to a PC.  I think I'm 90% there, but I'm missing something.  Here is what I have.

$user = whoami /upn
#Get-ADUser -Filter "EmailAddress -eq $user" | Select Name | ft -HideTableHeaders
Get-ADUser -Filter 'EmailAddress -eq "tfox@test.com"' | Select Name | ft -HideTableHeaders

#Test Output
Write-host
Write-host $user
write-host $name

This works great if I feed it a static email address.  I cannot for the life of me get it to use the $user variable.  I know it's probably a syntax error on my part.  Does anyone have any suggestions or a better way to skin a cat?

July 23rd, 2015 2:17pm

Hi,

You could do something like this:


Get-ADUser -Filter "SamAccountName -eq '$env:USERNAME'" | Select -ExpandProperty Name


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

Get-ADUser -Identity $env:USERNAME | Select -Expand Name
Keep it simple.
July 23rd, 2015 2:28pm

Awesome.  Those look like exactly what I need.  I've only tried the first one, but it gives me exactly what I wanted.
Free Windows Admin Tool Kit Click here and download it now
July 23rd, 2015 2:29pm

Get-ADUser -Identity $env:USERNAME | Select -Expand Name
Keep it simple.
July 23rd, 2015 6:25pm

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

Other recent topics Other recent topics