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.
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:
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
Returns: DomainName\UserName
Environment Variables in Powershell to get Current User
You can use Windows environment variables in powershell:
$env:UserName
$env:UserDomain
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:
[System.Environment]::UserName
[System.Environment]::DomainName
Can also be presented as:
[Environment]::UserName
[Environment]::DomainName
WHOAMI Windows Executable
Another method is to use WHOAMI Windows Executable:
whoami
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]