Getting Your Username in PowerShell
The fastest way to see your username in PowerShell is to type $env:USERNAME and press Enter. This displays the account name you are currently logged in as. The command works on any Windows machine running PowerShell, whether you are on version 5 or PowerShell 7, and returns the result when ready.
If you need to see the full domain and username together — useful on a work network where multiple domains exist — type $env:USERDOMAIN\$env:USERNAME instead. This shows both your domain name and your local username in the format "DOMAIN\username".
Both commands pull information Windows already knows about your current session, so there is no waiting or installation required. They work the same way whether you open PowerShell as a regular user or as an administrator.
Key Takeaways
- Type $env:USERNAME to see just your username, or $env:USERDOMAIN\$env:USERNAME to see your domain and username together.
- These commands work in any version of PowerShell on Windows without needing administrator rights.
- The whoami command is an alternative that shows the same information in a slightly different format.
- If you are running a script and need to store your username for later use, you can save it to a variable like $myusername = $env:USERNAME.
Using whoami as an Alternative
PowerShell also recognizes the whoami command, which is a Windows command that predates PowerShell. Type whoami and press Enter to see your username in the format "DOMAIN\username" automatically, without needing to combine two separate variables.
The whoami command is often faster to type if you only need the result once and do not plan to use it in a script. However, if you are writing a PowerShell script that needs to reference your username multiple times, the $env:USERNAME approach is cleaner because you can store it in a variable and reuse it.
Storing Your Username in a Variable
When you are writing a PowerShell script, you often need to use your username more than once. Instead of typing the command repeatedly, save it to a variable on the first line of your script: $myusername = $env:USERNAME. Then use $myusername anywhere else in the script where you need it.
This approach makes your script easier to read and faster to run. For example, if you are creating a log file that includes your username in the filename, you could write: $logfile = "C:\logs\$myusername-report.txt". PowerShell replaces $myusername with your actual username when the script runs.
Checking the Username of Another User's Session
If you need to find out which user is logged into a different computer on your network, you can use the Get-WmiObject command. Type Get-WmiObject -Class Win32_ComputerSystem -ComputerName COMPUTERNAME | Select-Object UserName, replacing COMPUTERNAME with the actual name of the other machine.
This command only works if you have network access to the other computer and the Windows Management Instrumentation service is running on it. On a work network, your IT department can tell you which computers you have permission to query this way.
Viewing All User Accounts on Your Machine
If you want to see every user account that exists on your computer — not just the one you are currently logged in as — use Get-LocalUser. This command lists all local accounts, including built-in accounts like Administrator and Guest, along with when each account was created and whether it is currently enabled.
Type Get-LocalUser and press Enter to see the full list. If you want to see details about only one specific account, type Get-LocalUser -Name USERNAME, replacing USERNAME with the account you want to check.
Why You Might Need Your Username
Knowing how to retrieve your username in PowerShell is useful for several reasons. You might need it to create a log file with your name in it, to verify which account is running a script, to troubleshoot permission issues, or to automate a task that requires you to reference your own account name.
On a work computer where you log in with a domain account, your username may be different from what you see on your desktop. Using PowerShell to check ensures you have the exact spelling and format that Windows uses internally, which matters when you are writing scripts or configuring network resources.
Frequently Asked Questions
What is the difference between $env:USERNAME and whoami?
Both show your username, but whoami includes your domain name automatically (DOMAIN\username), while $env:USERNAME shows only the username part. Use whoami for a quick check, and $env:USERNAME when you are writing a script and need just the username without the domain.
Can I use these commands if I am not an administrator?
Yes. Both $env:USERNAME and whoami work for any user account without administrator rights. They only read information about your own session, so Windows does not restrict them.
What if the command returns a blank result?
This is rare, but if $env:USERNAME returns nothing, try whoami instead. If both return blank, your PowerShell session may not have loaded environment variables correctly. Close PowerShell and open it again.
Can I find out what username a scheduled task is running under?
Yes, but you need to check the task itself, not PowerShell. Open Task Scheduler, find the task, right-click it, select Properties, and look at the General tab. The username appears under "Security options" or "Run as".
How do I change my username using PowerShell?
You cannot change your own username while logged in as that user. You must use Settings (on Windows 10 or 11) or Control Panel (on older versions), or ask your IT department if you are on a domain. PowerShell can only read usernames, not modify them.