How to Get the Current User Logged On with PowerShell (All the Ways)

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

This post is a detailed guide on how to get the current user logged onto your machine with PowerShell. It provides all of the options and their functions, as well as alternate methods if necessary. If you are looking for ways to log users in, this should be helpful!

The “powershell get current logged on user remote computer” is a PowerShell command that will show the current logged on user. This command can be run in one of two ways: remotely or locally.

How to Get the Current User Logged On with PowerShell (All the Ways)

When working with Windows, you’ll almost certainly need to know which user accounts are presently signed in to a computer at some time. Fortunately, PowerShell can be used to acquire current users on local or remote machines.

Do you have any passwords in your Active Directory that have been compromised? With Specops Password Auditor Free, you can find out.

You’ll learn how to utilize PowerShell to obtain current users on a computer in this article. Native commands,.NET classes, and Windows Management Instrumentation (WMI) specific cmdlets will all be covered.

Are you ready to find out who’s on your computer and what they’re up to? Let’s get started!

Prerequisites

The easiest way to learn in this lesson is to follow along with the examples. You’ll need a computer running any contemporary version of Windows, such as Windows 10, Windows Server 2012, or later, to do so.

This lesson will be performed on a machine called DC1 that is running Windows Server 2019.

Why Use PowerShell to Login to a Computer with Current Users?

There are methods to obtain current users signed on to a computer without using PowerShell, to be sure. After all, system administrators had to be able to accomplish this before PowerShell, right? If that’s the case, what’s the point of using PowerShell?

When doing the identical job in PowerShell, you may alter the results by treating them as objects. If you execute a script that asks for a user credential, PowerShell can dynamically generate the username, leaving you with just the password to enter in manually.

Another real-world example is saving a file with a list of people who are presently signed in to a server. PowerShell can extract the current users and save them as CSV, HTML, or XML files. This output may then be used in a variety of automation procedures.

There might be dozens of reasons to utilize PowerShell to acquire new users, but it all comes down to your needs. And this article will show you how to accomplish that aim in a variety of methods.

Using the Win32 ComputerSystem Class to Perform a Search

Let’s start with the Get-WMIObject and Get-CimInstance cmdlets from PowerShell. These cmdlets use the Windows Management Instrumentation (WMI) classes on a local or remote computer to query information, including the currently logged-on user.

Get-WMIObject is an obsolete (deprecated) cmdlet that can only be used with Windows PowerShell 5.1. Get-CIMInstance, on the other hand, is a modern, more secure cmdlet that is compatible with the most recent PowerShell versions.

Visit Get-CIMInstance vs. Get-WMIObject: What’s the Difference for additional information.

Invoke any cmdlet targeting the Win32 ComputerSystem class in PowerShell to retrieve the current user. The Username property is one of the attributes of the Win32 ComputerSystem class. To do so, launch PowerShell and perform the instructions shown below.

# Return just the Username attribute using the Get-WMIObject cmdlet (Get-WMIObject -ClassName Win32 ComputerSystem). Username # Return just the Username attribute using the Get-CimInstance cmdlet (Get-CimInstance -ClassName Win32 ComputerSystem). Username

Note: Using the Win32 ComputerSystem Class to Perform a Search will only return the username of who is logged on to the computer directly (console session). If the user logged on remotely (e.g., Remote Desktop), the username will be blank.

As illustrated below, both instructions get the same result. The command will return the domain and username if the current user is a domain user account (domainusername). However, the result for a local user account would be computerusername.

Using PowerShell to Get the Current User from WMI Using PowerShell to Get the Current User from WMI

Get-WMIObject and Get-CimInstance both offer a -computerName argument that takes the name of the computer to query. You may query the same information from a distant computer on the network if you use this argument.

However, if you just require the username and not the domain, you may divide the output using the split() function. To accomplish so, use PowerShell and perform the command below.

Concatenate, expand, format, and anything else Strings in PowerShell

# Using the backslash ” character as a delimiter, the command below will divide the username property into two. # The [1] at the end instructs the command to only return the index 1 result. # Because indexes begin with 0, specifying index 1 will display the result in the second place. Username = (Get-WMIObject -ClassName Win32 ComputerSystem). Split(”)[1]

As a consequence, PowerShell just returns the user component, as seen in the image below.

The username property is being split. The username property is being split.

How to Work with WMI in PowerShell is a related article.

Reading the Environment Variables in Windows

Another technique to acquire the current user on a computer using PowerShell is to get data from the environment variables. The operating system data, such as the current user’s username, is represented by the environment variables.

There are three methods to interact with the variables in the environment. The outcome would be the same regardless of whatever path you choose.

A Deep Dive into PowerShell and Environment Variables

The Env PowerShell Drive is a program that allows you to manage your environment with PowerShe

Environment variables are cached by PowerShell and made accessible via the Env: drive, a PowerShell Drive (PSDrive). PSDrives are data locations that may be accessed like a drive on a computer (for example, C:), but exclusively via PowerShell.

Understanding and Creating New PS Drives in PowerShell is a related article.

When you start a PowerShell session, the Env: drive is created automatically. Because Env: is a drive, you may access its contents using the Get-ChildItem cmdlet, which works similarly to listing the contents of a file system drive or folder.

Get-ChildItem: Listing Files, Registry, and Certificates is related to Get-ChildItem: Listing Files, Registry, and Certificates.

Run the command below in PowerShell to obtain the current user using the Env: drive.

Env:USERNAME Get-ChildItem

The predicted outcome is seen in the image below. The command returns the USERNAME item and its matching value, which is the presently logged-on user, as you can see.

Obtaining the object Env:USERNAME Obtaining the object Env:USERNAME

Modify the command as shown below and repeat it in PowerShell to just return the username value.

(Env:USERNAME Get-ChildItem).Value

The command just returns the username value as a string, as seen below.

Obtaining the value of Env:USERNAME Obtaining the value of Env:USERNAME

$env is a variable that can be used to change the environment.

Another option is to use the Env drive as a variable, rather than using the environment variables as a PSDrive and treating them as files on a drive. The syntax below may be used to refer to the environment variables.

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

To obtain the current user, execute the command below in PowerShell using the syntax above.

The command returns the username value, as seen in the image below.

Obtaining the value of the username variable Obtaining the value of the username variable

The Environment Class in.NET

PowerShell is a shell and object-oriented programming language. Objects are the building blocks of everything you do with PowerShell. When you execute a command, you’ll get an object as a result. If you define a variable, it is an object in and of itself. You get my drift.

Understanding PowerShell Objects (Back to Basics)

Similar to blueprints, these objects contain a structure that describes the data type they represent. These blueprints are referred to as.NET classes or just classes.

Getting Started with PowerShell Classes

The Environment class is the class that is related with the environment variables. So, how do you retrieve the current user using PowerShell’s Environment class?

Like $env is a variable that can be used to change the environment., the Environment class has a Username property, whose value is the current user’s username. To get the username property value, run the command below in PowerShell.

[System.Environment]::UserName

Obtaining the value of the environment username property Obtaining the value of the environment username property

The Environment class also contains a method for getting the value of an environment variable, in addition to the Username property. GetEnvironmentVariable is the name of the method.

Run the command below to retrieve the current user using this approach.

[System.Environment]::GetEnvironmentVariable(‘username’)

Obtaining the value of an environment variable Obtaining the value of an environment variable

Using the WindowsIdentity Class in.NET

Another.NET class you may use in PowerShell to retrieve the current user is the WindowsIdentity class. GetCurrentName is a method in this class that returns an object that represents the current Windows user.

Run the command below to acquire the current user’s information.

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

The command retrieves the current user object attributes, including the Name property, as seen below. The format of the Name property value follows the domainuser convention.

Obtaining the name of the current Windows user Obtaining the name of the current Windows user

Modify the preceding command to pick just the Name property and use the string splitting approach to return only the username component. To do so, copy and execute the command below in PowerShell.

([System.Security.Principal.WindowsIdentity]::GetCurrent().Name). Split(”) [1]

Only the username portion is returned. Only the username portion is returned.

The whoami Command is a command that allows you to find out who someone is.

The whoami command is another useful tool for rapidly obtaining the current user. This command is an executable program named whoami.exe that may be found in the percent WINDIR percent System32 folder.

Run the command below to acquire the current user’s username.

The command then displays the result in the format of the domain username, as seen in the image below.

Using whoami to get the username Using whoami to get the username

Apply the string split method you learned previously in this lesson to acquire the username without the domain by executing the command below.

Obtaining the username in the absence of the domain Obtaining the username in the absence of the domain

The whoami command may also return the fully qualified distinguished name (FQDN) and user principal name of the current user (UPN). If you want to execute a script that obtains the current user’s identity beyond the username, this command output could be useful.

To acquire the current user’s FQDN and UPN, use the PowerShell script below.

# whoami /fqdn Get the current user’s FQDN # whoami /upn Get the current user’s UPN

Getting the current user's FQDN and UPN Obtaining the FQDN and UPN of the current user

Note: If the currently logged-on user is a domain user, the FQDN and UPN values are only visible. The command will produce an error if you try to acquire these data for a local user, as illustrated below.

Error obtaining a non-domain user account's UPN and FQDN Error obtaining a non-domain user account’s UPN and FQDN

Querying with the command

If you’re a system administrator or a domain user, you’ve probably used the Distant Desktop Protocol (RDP) to connect to a remote workstation at least once, possibly to control resources or run programs. You may wish to know who is presently signed in and using system resources for any reason.

Related: The Best Free Windows Remote Desktop Connection Managers

Fortunately, Windows has a query command-line function that can display all presently logged-on users on a machine. The tool also displays whether the user signed in through a remote desktop session or locally.

The query command contains two parameters: session and user, which are used to obtain the logged-on users. The user parameter lists the users and their sessions, whereas the session parameter lists the computer sessions.

The command alias for query session is qwinsta, while the command alias for query user is quser.

To list the sessions on DC1, for example, use the command below.

# qwinsta /server:dc1 qwinsta /server:dc1 qwinsta /server:dc1 qwinsta /server

The output of the command is seen in the screenshot below. Two users signed in to the DC1 computer in this example, one through console and the other via RDP. As you can see, qwinsta returns every session, even non-user sessions.

Sessions for querying Sessions for querying

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

Run the user command to get a list of all users with active log-on sessions.

# queser /server:dc1 users on DC1

When you execute the command, you’ll see that, like the qwinsta program, quser returns the sessions and users straight quickly. This time, though, there will be no blank sessions.

IDLE TIME and Login TIME are two more columns that represent the users’ idle time and logon timestamp, respectively.

Inquiring about users Inquiring about users

Finally, when comparing the results of qwinsta and quser, quser is the most suitable PowerShell operation for getting current users.

Do you have any passwords in your Active Directory that have been compromised? With Specops Password Auditor Free, you can find out.

Conclusion

While there are several methods to retrieve the current users logged on in PowerShell, the optimal option depends on the result you want and how you’ll utilize it.

You can retrieve the logged-on users on a local or distant machine using native WMI-related cmdlets and the query command. Using.NET classes and environment variables, you may get the same results quickly, but only on your local machine.

Whether you want to execute a script or manually show results on the screen, there’s a PowerShell approach to acquire the current user that will meet your needs. What comes next? Perhaps learn how to write RegEx functions to parse query command results and convert them to PowerShell objects?

The “get current logged in user” is a command-line tool that allows users to get the current user logged on with PowerShell. This command can be used for all the ways, including getting the currently logged on user and getting a list of currently logged on users.

Related Tags

  • get logged in users powershell
  • get logged on user remote computer
  • find where a user is logged on in a domain powershell
  • powershell get all logged on users
  • powershell get upn of current user