---
title: "Using the Dynamic PowerShell to Get Current Users"
description: "Learn how use PowerShell to get current users in this step-by-step tutorial walking you through how to do so many different ways!"
canonical: "https://adamtheautomator.com/powershell-get-current-user/"
---

# Using the Dynamic PowerShell to Get Current Users

> Learn how use PowerShell to get current users in this step-by-step tutorial walking you through how to do so many different ways!

Source: https://adamtheautomator.com/powershell-get-current-user/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Using the Dynamic PowerShell to Get Current Users](https://adamtheautomator.com/wp-content/uploads/2021/08/How-to-Get-the-Current-User-Logged-On-with-PowerShell-All-the-Ways.jpg)

# Using the Dynamic PowerShell to Get Current Users

[![](https://secure.gravatar.com/avatar/9a14f10ff1b1ec7d790d34f5b559e4d3de2d31b172e6ef266dfd8b479174d97b?s=192&d=mm&r=g)June Castillote](https://adamtheautomator.com/author/june/)13 August 20218 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Powershell Administration](/tag/powershell-administration/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Why Use PowerShell to Get Current Users Logged On to a Computer?](#why-use-powershell-to-get-current-users-logged-on-to-a-computer)
*   [Querying the Win32\_ComputerSystem Class](#querying-the-win32_computersystem-class)
*   [Reading the Windows Environment Variables](#reading-the-windows-environment-variables)
*   [The Env PowerShell Drive](#the-env-powershell-drive)
*   [The $env Variable](#the-env-variable)
*   [The .NET Environment Class](#the-net-environment-class)
*   [Using the .NET WindowsIdentity Class](#using-the-net-windowsidentity-class)
*   [Using the whoami Command](#using-the-whoami-command)
*   [Using the query Command](#using-the-query-command)
*   [Conclusion](#conclusion)

When you work with Windows, at some point, you’ll probably need to find out which user accounts are currently logged on to a computer. Luckily, you can use PowerShell to get current users on local or remote computers.

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

> _Do you have compromised passwords in your Active Directory? Find out with [Specops Password Auditor Free](https://specopssoft.com/product/specops-password-auditor/?utm_source=ATA&utm_medium=referral&utm_campaign=ATA%20promo%202021&utm_content=SPA%20in-article%20link)._

In this tutorial, you’ll learn several ways to use PowerShell to get current users on a computer. The methods you learn will include native commands, [.NET classes](https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/types/classes), and [Windows Management Instrumentation (WMI)](https://docs.microsoft.com/en-us/windows/win32/wmisdk/wmi-start-page) specific cmdlets.

Are you ready to find out who’s lurking on your computer? Let’s begin!

## Prerequisites

In this tutorial, you’ll best learn by following the examples. And to do so, you’ll need a computer that runs on any modern version of Windows, such as Windows 10, Windows Server 2012, or later.

This tutorial will use a computer running Windows Server 2019 named _DC1_.

## Why Use PowerShell to Get Current Users Logged On to a Computer?

Admittedly, there are ways to get current users logged on to a computer without PowerShell. After all, system admins have to have had a way to do so even before PowerShell, right? If so, why use PowerShell?

Doing the same task in PowerShell benefits from treating the results as objects that you can manipulate. For example, if you run a script that prompts for the user credential, PowerShell can programmatically populate the username, and you only need to type in the password manually.

Another real-world example is exporting the list of users currently logged on to a server to a file. It is by using PowerShell to get the current users then export the result to [CSV](https://adamtheautomator.com/csv-file/), [HTML](https://adamtheautomator.com/html-report/), or [XML](https://atablog.hashnode.dev/export-clixml-and-import-clixml-saving-objects-to-xml) files. Afterward, various automation workflows can use this output.

There could be thousands of reasons to use PowerShell to get current users, but the end game all boils down to your requirements. And this tutorial will teach you the different ways to achieve that goal.

## Querying the Win32\_ComputerSystem Class

Let’s start with the PowerShell native cmdlets called [`Get-WMIObject`](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-wmiobject?view=powershell-5.1) and [`Get-CimInstance`](https://docs.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance?view=powershell-7.2&viewFallbackFrom=powershell-7.1). These cmdlets allow you to query information, including the currently logged-on user, using the [Windows Management Instrumentation (WMI)](https://docs.microsoft.com/en-us/windows/win32/wmisdk/about-wmi) classes on a local or remote computer.

> _`Get-WMIObject` is the older (deprecated) cmdlet and is only available up to Windows PowerShell 5.1. Conversely, `Get-CIMInstance` is the newer, more secure cmdlet and is available up to the [latest PowerShell versions](https://docs.microsoft.com/en-us/powershell/scripting/overview?view=powershell-7.2&viewFallbackFrom=powershell-7.1)._

> _To learn more, visit [Get-CIMInstance Vs. Get-WMIObject: What’s The Difference?](https://www.ipswitch.com/blog/get-ciminstance-vs-get-wmiobject-whats-the-difference)_

To use PowerShell to get the current user, invoke either cmdlet targeting the [Win32\_ComputerSystem class](https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-computersystem). The Win32\_ComputerSystem class includes various properties, including the `Username` property. To do so, open a PowerShell window and run the commands below.

```powershell
# Using the Get-WMIObject cmdlet and return only the Username property
(Get-WMIObject -ClassName Win32_ComputerSystem).Username

# Using the Get-CimInstance cmdlet and return only the Username property
(Get-CimInstance -ClassName Win32_ComputerSystem).Username
```

> _Note: Querying the Win32\_ComputerSystem class [will only return the username of who is logged on to the computer directly (console session)](https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-computersystem?redirectedfrom=MSDN#:~:text=In%20a%20terminal%20services%20session%2C%20UserName%20returns%20the%20name%20of%20the%20user%20that%20is%20logged%20on%20to%20the%20console%20not%20the%20user%20logged%20on%20during%20the%20terminal%20service%20session). If the user logged on remotely (e.g., [Remote Desktop](https://support.microsoft.com/en-us/windows/how-to-use-remote-desktop-5fe128d5-8fb1-7a23-3b8a-41e636865e8c)), the username will be blank._

Both commands return the same result, as shown below. If the current user is a domain user account, the command will return the domain and username (`domain\username`). But for a local user account, the output would be `computer\username`.

![Querying WMI in PowerShell to Get Current Users ](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-11T133649.626.png)

Querying WMI in PowerShell to Get Current Users

> _`Get-WMIObject` and `Get-CimInstance` have a parameter called `-computerName`, which accepts the computer’s name to query. Using this parameter means that you can query the same information from a remote machine in the network._

But if you need to get only the username without the domain, a solution is to split the output using the [`split()` method](https://adamtheautomator.com/powershell-strings/#Splitting_Strings_with_the_Split_Method) . To do so, run the command below in PowerShell.

Related:[Concatenate, Expand, Format, and All Things PowerShell Strings](https://adamtheautomator.com/powershell-strings/)

```powershell
# The command below will split the username property into two using the backslash '\' character as the delimiter.
# The [1] part at the end tells the command to return only the result at index 1.
# Indexes start with 0, which means specifying index 1 will show the result at the second position.
((Get-WMIObject -ClassName Win32_ComputerSystem).Username).Split('\')[1]
```

As a result, PowerShell returns the user part only, like the screenshot below.

![Splitting the username property](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-11T133857.037.png)

Splitting the username property

Related:[How to Work with WMI in PowerShell](https://adamtheautomator.com/powershell-wmi/)

## Reading the Windows Environment Variables

Another way to use [PowerShell](https://adamtheautomator.com/powershell-wmi/) to get the current user on a computer is by retrieving values from the [environment variables](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-7.2&viewFallbackFrom=powershell-7.1). The environment variables represent the operating system data, including the current user’s username, among others.

There are three ways to interact with the [environment variables](https://adamtheautomator.com/powershell-environment-variables/). Whichever way you choose would return the same result.

### The Env PowerShell Drive

PowerShell caches environment variables and makes them available through a [PowerShell Drive (PSDrive)](https://docs.microsoft.com/en-us/powershell/scripting/samples/managing-windows-powershell-drives?view=powershell-7.2&viewFallbackFrom=powershell-7.1) called the `Env:` drive. PSDrives are data locations that you can access like a drive on the computer (e.g., _C:_), but are only accessible inside PowerShell.

Related:[Understanding and Building New PS Drives in PowerShell](https://adamtheautomator.com/new-psdrive/)

PowerShell automatically creates the `Env:` drive as soon as you open a PowerShell session. And because `Env:` is a drive, you can get its contents by invoking the [`Get-ChildItem`](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7.2&viewFallbackFrom=powershell-7.1) cmdlet, similar to how you would list the contents of a file system drive or folder.

Related:[Get-ChildItem: Listing Files, Registry, and Certificates](https://adamtheautomator.com/get-childitem/)

Using the Env: drive In PowerShell, get the current user by running the command below.

```powershell
Get-ChildItem Env:\USERNAME
```

The screenshot below shows the expected result. As you can see, the command returns the `USERNAME` item and its corresponding value, which is the currently logged-on user.

![Getting the Env:\\USERNAME item](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-11T134106.326.png)

Getting the Env:\\USERNAME item

To return the username value only, modify the command as you see below and rerun it in PowerShell.

```powershell
(Get-ChildItem Env:\USERNAME).Value
```

And as you can see below, the command only returns the username value as a string.

![Getting the Env:\\USERNAME value](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-11T134947.498.png)

Getting the Env:\\USERNAME value

### The $env Variable

Aside from accessing the environment variables as a PSDrive treating them as files in a drive, another way is accessing the `Env` drive as a variable. Meaning, you can reference the environment variables using the syntax below.

```powershell
# The $ sign indicates a variable
# env is the Env drive
# <variable> is the variable name you want to get
$env:<variable>
```

Based on the said syntax, run the command below in PowerShell to get the current user.

```powershell
$env:username
```

As the screenshot below shows, the command returns the username value.

![Getting the username variable value](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-11T135028.457.png)

Getting the username variable value

### The .NET Environment Class

PowerShell is an object-oriented language and shell. Essentially everything that you work on within PowerShell is an object. When you run a command, the output is an object. If you declared a variable, that variable is an object itself. You get the idea.

Related:[Back to Basics: Understanding PowerShell Objects](https://adamtheautomator.com/powershell-objects/)

These objects have a structure that defines the data type they represent, similar to blueprints. And these blueprints are called [.NET classes](https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/types/classes) or simply classes.

Related:[PowerShell Classes: Getting Started](https://adamtheautomator.com/powershell-classes/)

As for the environment variables, the class associated with it is the [Environment](https://docs.microsoft.com/en-us/dotnet/api/system.environment?view=net-5.0) class. So how do you use the _Environment_ class in PowerShell to get the current user?

Like the `$env` variable, the Environment class has a [`Username`](https://docs.microsoft.com/en-us/dotnet/api/system.environment?view=net-6.0&viewFallbackFrom=net-5.0.username) property, whose value is the current user’s username. To get the username property value, run the command below in PowerShell.

```powershell
[System.Environment]::UserName
```

![Getting the environment username property value](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-11T135142.442.png)

Getting the environment username property value

Apart from the `Username` property, the Environment class also has a method to get the value of an environment variable. The method’s name is [`GetEnvironmentVariable`](https://docs.microsoft.com/en-us/dotnet/api/system.environment?view=net-6.0&viewFallbackFrom=net-5.0.getenvironmentvariable).

To get the current user using this method, run the command below.

```powershell
[System.Environment]::GetEnvironmentVariable('username')
```

![Getting the environment variable value](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-11T135239.557.png)

Getting the environment variable value

## Using the .NET WindowsIdentity Class

The [WindowsIdentity](https://docs.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity?view=net-6.0&viewFallbackFrom=net-5.0) class is another .NET class you can invoke in PowerShell to get the current user. This class has a method called [`GetCurrentName`](https://docs.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity?view=net-6.0&viewFallbackFrom=net-5.0.getcurrent), which returns an object that represents the current Windows user.

To get the current user details, run the command below.

```powershell
[System.Security.Principal.WindowsIdentity]::GetCurrent()
```

As you can see below, the command returns the current user object properties, including the `Name` property. The `Name` property value’s format follows the `domain\user` convention.

![Getting the current Windows user](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-11T135402.407.png)

Getting the current Windows user

To return the username part only, modify the previous command to select only the `Name` property, and apply the string splitting technique. To do so, copy the command below and run it in PowerShell.

```powershell
([System.Security.Principal.WindowsIdentity]::GetCurrent().Name).Split('\')[1]
```

![Returning only the username part](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-11T135448.924.png)

Returning only the username part

## Using the `whoami` Command

Another handy command that quickly lets you get the current user is the `whoami` command. This command is an executable file that you can find in the _%WINDIR%\\System32_ folder with the file name `whoami.exe`.

To get the current user’s username, run the command below.

```powershell
whoami
```

The command then returns the result in the `domain username` format, such as the screenshot below.

![Getting the username using whoami](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-11T135627.456.png)

Getting the username using whoami

To get the username without the domain, apply the string split technique you learned earlier in this tutorial by running the command below.

```powershell
(whoami).Split('\')[1]
```

![Getting the username without the domain](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-11T135656.900.png)

Getting the username without the domain

The `whoami` command can also return the current user’s [fully qualified distinguished name (FQDN)](https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ldap/distinguished-names) and the [user principal name (UPN)](https://social.technet.microsoft.com/wiki/contents/articles/52250.active-directory-user-principal-name.aspx). This command output could be helpful if you intend to run a script that retrieves the current user’s identity beyond the username.

Run the command below in PowerShell to get the current user’s FQDN and UPN.

```powershell
# Get the current user's FQDN
whoami /fqdn

# Get the current user's UPN
whoami /upn
```

![Getting the current user's FQDN and UPN](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-11T135729.552.png)

Getting the current user’s FQDN and UPN

> _Note: The FQDN and UPN values are only available if the currently logged-on user is a domain user. If you attempt to get these values for a local user, the command will return an error, as shown below._

![Error getting the UPN and FQDN of a non-domain user account](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-11T135803.994.png)

Error getting the UPN and FQDN of a non-domain user account

## Using the `query` Command

If you’re a system admin or a domain user, chances are you’ve logged on to a remote machine via the [Remote Desktop Protocol (RDP)](https://docs.microsoft.com/en-us/troubleshoot/windows-server/remote/understanding-remote-desktop-protocol) at least once, perhaps to manage resources or use applications. Whatever the reason, you would want to know who is currently logged on and using system resources.

Related:[The Top Free Remote Desktop Connection Managers for Windows](https://adamtheautomator.com/remote-desktop-connection-manager/)

Luckily, Windows has a built-in command-line tool called [`query`](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/query) that can list all the currently logged-on users on a computer. The command also shows you if the user logged on via a remote desktop session or locally to the computer.

The `query` command has two parameters pertinent to getting the logged-on users; [`session`](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/query-session) and [`user`](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/query-user). The `session` parameter lists the computer sessions, while the `user` parameter lists the users and their sessions.

> _The `query session` command has an alias called `qwinsta`, while the command alias for `query user` is `quser`._

For example, run the command below to list the sessions on _DC1_.

```powershell
# query sessions on DC1
qwinsta /server:dc1
```

The screenshot below shows the command’s output. In this example, two users logged on to the DC1 computer—one in the console session and another via RDP. As you can see, `qwinsta` returns all the sessions, even the non-user ones.

![Querying sessions](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-11T140021.413.png)

Querying sessions

> _If you run `qwinsta` or `quser` by itself without the `/server:<computer>` argument, either command will query the local computer by default._

Next, to list all users with existing log-on sessions, run the `user` command below.

```powershell
# query users on DC1
quser /server:dc1
```

After running the command, you’ll notice right away that `quser` returns the sessions and users like the `qwinsta` command. But this time, there are no blank sessions.

There are also two additional columns: `IDLE TIME`, which shows the users’ idle time, and `LOGON TIME` showing the users’ logon timestamp.

![Querying users](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-11T140057.954.png)

Querying users

In the end, comparing the `qwinsta` and `quser` results, `quser` is the more appropriate command in PowerShell to get current users.

> _Do you have compromised passwords in your Active Directory? Find out with [Specops Password Auditor Free](https://specopssoft.com/product/specops-password-auditor/?utm_source=ATA&utm_medium=referral&utm_campaign=ATA%20promo%202021&utm_content=SPA%20in-article%20link)._

## Conclusion

While there are many ways in PowerShell to get the current users logged on, which method is best depends on the result you intend to get and how you will use it.

The native WMI-related cmdlets and the `query` command allow you to get the logged-on users on a local or remote computer. Using .NET classes and environment variables offers quick ways to retrieve the same results but only on the local computer.

Whether you plan to run a script or manually display results on the screen, there’s a method that would fit your requirements to use PowerShell to get the current user. What’s next? Perhaps learn how to [parse the query command results](https://gist.github.com/junecastillote/a2f7fd732ccc38f05cbc9e5d761d5594) and convert them to [PowerShell objects](https://adamtheautomator.com/powershell-objects/) by writing [functions](https://adamtheautomator.com/powershell-functions/) with [RegEx](https://adamtheautomator.com/powershell-regex/)?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-get-current-user%2F&text=Using%20the%20Dynamic%20PowerShell%20to%20Get%20Current%20Users)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-get-current-user%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-get-current-user%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/09/How-to-Execute-PowerShell-Azure-Functions-with-HTTP-Triggers.jpg)

### [How to Execute PowerShell Azure Functions with HTTP Triggers](/azure-functions-powershell-2/)

Learn how to execute and test Azure Functions in PowerShell via HTTP using both the Azure Portal and PowerShell’s Invoke-RestMethod cmdlet.

![](https://adamtheautomator.com/wp-content/uploads/2022/09/Building-PowerShell-Azure-Functions-with-VS-Code-Series.png)

### [Building PowerShell Azure Functions with VS Code \[Series\]](/azure-functions-powershell-1/)

Running PowerShell in Azure Functions gives your scripts infinite resources and flexibility. Learn how to cloudify your PowerShell scripts with Azure Functions!

![](https://adamtheautomator.com/wp-content/uploads/2022/02/Autonomous-Persona-Management-using-1E-Tachyons-PowerShell-Toolkit.jpg)

### [Autonomous Persona Management using 1E Tachyon’s PowerShell Toolkit](/1e-powershell-3/)

Learn how to manage thousands of endpoints with dynamic tagging capabilities with 1E’s Tachon.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
