Local Logged In User

Welcome Forums General PowerShell Q&A Local Logged In User

This topic contains 3 replies, has 3 voices, and was last updated by

3 years, 9 months ago.

  • Author
  • #27811

    Points: 1
    Rank: Member

    I'm trying to come up with a way to get a display or friendly name for a local user logged into their PC. This is part of a larger project, and I need to be able to write some basic information to a database.

    Here's what I have so far. I cannot for the life of me figure out what I am doing wrong. I am assuming it is a syntax error, as it works when I feed it information rather than a variable.

    $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 $user
    write-host $name

  • #27812

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    The outermost set of quotation marks determine whether PowerShell replaces variables or not. In your example, they're single quotes, so $user is being passed literally rather than being replaced.

  • #27813

    Points: 1
    Rank: Member

    Thanks, that gets me further along.

  • #27814

    Points: 639
    Helping Hand
    Rank: Major Contributor

    In VBScript, there was a COM object called ADSystemInfo that was an easy way to get AD information for the logged on user. Check this out as a Powershell way:


    Keep in mind that if this is running on the client, that Windows RSAT tools need to be installed in order to use Get-ADUser as you will need the ActiveDirectory module. So, typically, ADSI is leveraged to find user information in those instances which is provided I the above link. The ADSI there can be shortened a bit, but validate it is working as expected first.

The topic ‘Local Logged In User’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort