How to Get the Current UserName in Windows PowerShell

There are many ways to Get the Current Logged in Username in Windows PowerShell. In this article let’s look at some of the easiest and best ways to fetch the current username using PowerShell.

Get the Current UserName in Windows PowerShell Command

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name # Returns Domain name and Username


# Output
# Administrator

The above command will return the current logged in username as the output.

Command to Fetch Current User Domain and Machine Name in PowerShell


# Output
# Gamerz
# Askmein

The above commands will return you the Machine Name and the Domain name of the logged in user.There are few other techniques which you could use to get the Username. Below are some of the commands with the output.

PowerShell Command to Get the Username

If you want to get the current Logged In username then the best command to use is $(Get-WMIObject -class Win32_ComputerSystem | select username).username 

I personally do not recommend using $env:username because it will fetch the username from the environment variables and we know that environment variables can be easily altered.

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name # Returns Domain name and Username

$env:username # Returns username

$(whoami) # Returns domain name and username

$(Get-WMIObject -class Win32_ComputerSystem | select username).username

# Returns the name of the Logged in user


Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Powershell Concatenate String

PowerShell Concatenate String

Table of Contents Hide Introduction to PowerShell Concatenate StringHow to Concatenate Strings and Variables in PowerShell#1. Expanding Strings#2. Substitution or string.Format approach#3 Join Operator Microsft PowerShell scripting is little different from…
View Post

How to Comment in PowerShell Script

Table of Contents Hide Single Line Comment in PowerShellMultiLine Comments in PowerShellSyntax for  Comment Based Help in FunctionsSyntax for Comment-Based Help in Scripts Like any other programming language, PowerShell also…
View Post
Get Powershell Version

Get PowerShell Version

Table of Contents Hide How to Get PowerShell Version on windows?Method 1: Using $PSVersionTableMethod 2: Using Get-Host Method 3: Using $Host.VersionConclusion The best way to get PowerShell version is by running…
View Post