Reading Last Logged-On User and Other Registry Values

by Dec 2, 2020

Reading some registry values with PowerShell is typically a snap: simply use Get-ItemProperty. This snippet of code reads the Windows operating system details, for example:

$Path = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"

Get-ItemProperty -Path $Path |
Select-Object -Property ProductName, CurrentBuild, ReleaseId, UBR

The result looks like this:

ProductName    CurrentBuild ReleaseId UBR
-----------    ------------ --------- ---
Windows 10 Pro 19042        2009      630   

Unfortunately, Get-ItemProperty really works well only in combination with Select-Object because the cmdlet always adds a number of properties. If you wanted to read just a single registry value, i.e. the last logged-on user, this always requires the combination of the two:

$Path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI"

Get-ItemProperty -Path $Path |
Select-Object -ExpandProperty LastLoggedOnUser

Alternatively, you can store the result of Get-ItemProperty in a variable and access individual registry values with the dot notation:

$Path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI"

$values = Get-ItemProperty -Path $Path

If this call doesn’t help you launching Windows Terminal from a different user account, then in our upcoming tip we show how you can turn the app into a portable app that is no longer managed by Windows. Instead, this way you can run it by any user, including elevated accounts. Stay tuned.

$Path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI"

$key = Get-Item -Path $Path

Twitter This Tip! ReTweet this Tip!