A user rings the service desk and nobody knows which machine they are sitting at. If you run Microsoft Configuration Manager (ConfigMgr, still widely called SCCM), you already have that answer in the database. This guide gives you a prompted query that asks for a username and returns the computer that user last logged on to, then shows quicker alternatives in the console, PowerShell, SQL and CMPivot, and explains where the data comes from so you know how far to trust it.
Where “last logged on user” comes from
ConfigMgr stores the last logon user name against each device record. It is populated from the client’s heartbeat discovery and from Active Directory System Discovery, so it reflects the last interactive logon reported, not necessarily who is using the machine right now. A device that has been offline for weeks will still show the last person who used it. For live information, use CMPivot, covered further down.
The prompted WQL query
In the console go to Monitoring > Queries, right-click and choose Create Query. Give it a name such as “Find device by last logged on user”, set the object type to System Resource, click Edit Query Statement, then Show Query Language and paste the following:
select SMS_R_System.LastLogonUserName, SMS_R_System.LastLogonUserDomain, SMS_R_System.Name, SMS_R_System.Client, SMS_R_System.IPAddresses, SMS_R_System.IPSubnets, SMS_R_System.MACAddresses, SMS_R_System.OperatingSystemNameandVersion, SMS_R_System.ResourceDomainORWorkgroup, SMS_R_System.SMSAssignedSites, SMS_R_System.AgentName, SMS_R_System.AgentSite, SMS_R_System.AgentTime, SMS_R_System.SystemRoles, SMS_R_System.SMSUniqueIdentifier, SMS_R_System.ResourceId, SMS_R_System.ResourceNames, SMS_R_System.NetbiosName from SMS_R_System where SMS_R_System.LastLogonUserName = ##PRM:SMS_R_System.LastLogonUserName##
When you run it, the console prompts for a value. Enter the username on its own, without the domain, for example jbloggs. The results list every device where that user was the last to log on, which is useful when someone has more than one machine.
A slimmer version
Most of the time you only need a handful of columns. This version also uses like, so you can enter part of a name with % as a wildcard, for example jblog%:
select SMS_R_System.Name, SMS_R_System.LastLogonUserName, SMS_R_System.IPAddresses, SMS_R_System.OperatingSystemNameandVersion from SMS_R_System where SMS_R_System.LastLogonUserName like ##PRM:SMS_R_System.LastLogonUserName##
Related queries
If you would rather browse the whole list, use our last logged on user query for all systems. To go the other way and find the user from a computer name, see find the last logged on user by NetBIOS name.
Faster options in current ConfigMgr
Console search
In Assets and Compliance > Devices, the device list can show the Last Logon User and Primary User(s) columns. Right-click a column header to add them, then use Add Criteria in the search bar to filter on Last Logon User. For a one-off lookup this is often quicker than a saved query.
PowerShell
From a ConfigMgr PowerShell session (connect from the console menu), you can filter devices by the same property:
Get-CMDevice | Where-Object LastLogonUser -eq 'jbloggs' |
Select-Object Name, LastLogonUser, PrimaryUser, CurrentLogonUser
On a large estate, pulling every device is slow, so a SQL query or the console search is better for regular use.
SQL for reports
If you want this in an SSRS report or Power BI, query the site database views directly:
SELECT Name0 AS ComputerName, User_Name0 AS LastLogonUser, User_Domain0 AS Domain FROM v_R_System WHERE User_Name0 = @UserName
Our guide to creating an SSRS report from Configuration Manager data shows how to turn that into a report with a username parameter, and the SCCM SQL and WQL query library has many more ready-made queries.
CMPivot for who is logged on now
CMPivot queries online clients in real time. Run it against a collection and use the User entity to see who is currently signed in to each device:
User | where UserName contains 'jbloggs'
It only returns results from devices that are online, but the data is live rather than whatever was last reported.
User device affinity is often the better answer
Last logon is a rough guide. Hot desks, shared PCs and IT staff logging on to fix something all muddy it. If you have user device affinity configured, the Primary User field tells you who the machine actually belongs to. Affinity can be set automatically from usage thresholds in client settings, or manually by an administrator. For targeting software at people rather than machines, it is worth setting up properly, and our guide on using SCCM collections and query rules effectively covers how to build collections around it.
FAQ
Why does the query return nothing?
Check you entered the username without the domain prefix, and that the device has a working client or has been found by AD System Discovery recently. Also try the like version with a % wildcard in case of a typo.
Does this work in Intune?
Not as written. For co-managed devices the ConfigMgr data still applies. For cloud-only devices, the Intune admin centre shows the primary user on each device and the device list can be filtered by user.
Gear We Recommend
Testing configs is easier with a dedicated admin machine set up right. Here’s the kit we use.
Browse our Windows Admin Toolkit picks on AmazonAs an Amazon Associate, TechyGeeksHome earns from qualifying purchases.
Discover more from TechyGeeksHome
Subscribe to get the latest posts sent to your email.