r/PowerShell • • Oct 22 '26

Question Get current logged on User from System context script

I need to query the current logged on user and then write the info in HKLM registry for branding. Does anyone have a script or idea how this will work when the script is deployed from SCCM/Intune with System Context?

5 Upvotes

13 comments sorted by

12

u/PinchesTheCrab Oct 22 '26 edited Oct 22 '26 • Mindblown

Look for the owner(s) of explorer.exe processes.

Invoke-CimMethod -Query 'select * from win32_process where name = "explorer.exe"' -MethodName GetOwner

1

u/TechEUC-Atoofa Oct 23 '26

Thanks this worked well

4

u/cloudAhead Oct 22 '26

Not guaranteed to get one result from that query when running on a server, especially servers running the Remote Desktop Services role.

3

u/PinchesTheCrab Oct 22 '26

Isn't that a good thing? A single result would be inaccurate in that case.

2

u/cloudAhead Oct 22 '26

Agree, depends on how OP decides to handle more than one result.

2

u/AbfSailor Oct 22 '26

Same method I've used for years, works very well.

6

u/trickiegt2 Oct 22 '26

'Quser' is my goto, shows active / inactive sessions, their ids & console / remote connection If I remember correctly quser is shorthand for queryuser

3

u/AcceptablePlay Oct 22 '26

I always had following lines to help me when in was still working with SCCM. First part should do the trick. Do test it, I'm on mobile and can't verify :)

$User = New-Object System.Security.Principal.NTAccount((Get-CimInstance -ClassName Win32_ComputerSystem).UserName)

$UserSID = ($User.Translate([System.Security.Principal.SecurityIdentifier])).Value

$LoggedOnUserProfilePath = Get-ItemPropertyValue -Path (Join-Path -Path 'Registry::HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' -ChildPath $UserSid) -Name ProfileImagePath

2

u/theballygickmongerer Oct 22 '26

You have SCCM, use the primary user from reports. You should be easily able to build a query and write the reg value.

1

u/Constant-Position601 Oct 23 '26

I have a script that finds this, but it’s using the outdated use of “Win32_Process”.

I was going to test using this:

Get-Process -Name explorer -IncludeUserName

1

u/ZovexUK Oct 24 '26

Does BGInfo not cover this, depending what you're using it for / branding