Get user console from system Account with PowerShell Administration Scripts
Get user console – owerShell script to retrieve the currently logged-in user and send messages using system accounts.
Useful external reference: Microsoft Learn.
Get user console Overview
This PowerShell script allows you to get the user currently logged into a computer using the system account.Get-WmiObject Win32_Process -f 'Name="explorer.exe"' |% getowner |% user

Get user console Overview
$sessionId = (Get-WmiObject Win32_Process -Filter 'Name="explorer.exe"').SessionId | Select-Object -Unique msg.exe $sessionId "$message"
