Several ways in Powershell – Get Current User logged in

Why getting current logged in user

There are several ways in Powershell to get current user that is using the system. Which can be helpful in domain environment. In addition, if you’re running a script with credentials, you can insert the current logged username and domain in Credential variable (which will leave you to input only the password) for usage during whole script.

Affiliate: Experience limitless no-code automation, streamline your workflows, and effortlessly transfer data between apps with

Powershell – Get Current User logged in Methods

GetCurrent method of WindowsIdentity .NET Class

The best option is to use the GetCurrent method of WindowsIdentity .NET Class. Works most of the time, including Linux:


Returns: DomainName\UserName

Environment Variables in Powershell to get Current User

You can use Windows environment variables in powershell:


The problem with environmental variables – they can be altered by the user of the computer that the script is running on. So, if you want a hundred percent compatibility, the first method is preferred.

.NET Environment Class

Equivalent to the above method is using .NET Environment Class of System Namespace:


Can also be presented as:


WHOAMI Windows Executable

Another method is to use WHOAMI Windows Executable:


Returns as in first method “DomainName\UserName”. This method will be used only on Windows operating systems and servers. Only these systems will have the “whoami.exe” executable present. Another downside of the executable is it needs to run in order to receive the result. It takes several milliseconds more in order to show the result compared to the first method that is an integrated part of Powershell. Since “whoami” is an executable – it can run also in CMD Command Line and any other interface.

Get-WMIObject and Get-CimInstance Powershell Cmdlets

Query WMI with Get-WMIObject Powershell Cmdlet:

(Get-WMIObject -ClassName Win32_ComputerSystem | select username).username

Returns: “DomainName\UserName”.

Query WMI with Get-CimInstance Powershell Cmdlet:

(Get-CimInstance -ClassName Win32_ComputerSystem | select username).username

Returns: “DomainName\UserName”.
“Get-CimInstance” was introduced in Powershell 3.0 as part of CIM APIs implementation in Powershell. Basically WMI is Microsoft’s implementation of CIM.

Powershell – Get Current User logged in – Real Life examples of usage in Credential variable

Usage within Get-Credential Cmdlet:

# Get current user
$CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
# Set Credential
$Credential = Get-Credential -UserName $CurrentUser -Message "Input Credential"

“-UserName” property of “Get-Credential” Cmdlet will input the variable in the Username textbox of the Credential window and “-Message” will be a text that is shown in a label-box above the username textbox.

Another example is dividing in to appropriate variables of separated Domain and User Name of the current user:

# Get current user
$CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
# Set the variable to the first string before the "\" character
$CurrentDomainName = $CurrentUser.split("\")[0]
# Set the variable to the second string after the "\" character
$CurrentUserName = $CurrentUser.split("\")[1]

Or you can use Hashtables:

# Define Hashtable
$CurrentUser = @{}
# Define Current user variable
$CurrentUser.Full = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
# Split the variables to appropriate values
$CurrentUser.Domain = $CurrentUser.Full.split("\")[0]
$CurrentUser.Name = $CurrentUser.Full.split("\")[1]

1 thought on “Several ways in Powershell – Get Current User logged in”

  1. You can strip the domain from username output by creating another variable– below assumes is the domain:
    $longuser=(Get-CimInstance -ClassName Win32_ComputerSystem | select username).username
    $user=$longuser -Replace ‘CONTOSO’
    -This should return \user
    -you’ve got to be better at powershell and escaping regex than I am to get rid of the \, it didn’t matter for me because I used this to create a userprofile file path — so my next command was simply something like:
    echo C:\users$user\appdata\whatever\easyway\around\unique-user-path\

    so all in all, I use the two lines to define variables, then echo to spit it out how I want it, I then take that file path output and use it as a destination in a script that updates the MS Excel PERONAL.XLSB macro file. Can use GPO, but powershell scripts allow me to add manual procedures that auto-update macro file status for various users. Another few scripts use the same method to place macro files for users. Either way, this method solves a ton of problems for me – and hopefully others here.
    All together:
    $longuser=(Get-CimInstance -ClassName Win32_ComputerSystem | select username).username; $user=$longuser -Replace ‘CONTOSO’; echo C:\users$user\appdata\roaming\microsoft\excel\xlstart\personal.xlsb
    –uses ; to string commands into a single line.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.