Available for all Windows PowerShell versions
To see who the owner of a specific process is, how many instances of improved processes are there, you can try the following code:
$ProcessName = ''
(Get-WmiObject -Query "select * from Win32_Process where name='$ProcessName'").GetOwner().User
Note: There are many different ways to get the currently logged in user, but the methods are different due to different versions of your operating system. It's okay if you use a graphical user interface, but if it's a non-user interface Core server, the script may not detect the user currently connected to the host.
The above example returns the owner of all "" processes. If you have administrator privileges and are logged in remotely, inactive users may also be included in the user list. Because what is opened on each desktop may belong to different sessions and different users.
If the Sort-Object command is used, duplicates can be excluded very easily.
$ProcessName = ''
(Get-WmiObject –Query "select * from Win32_Process where name='$ProcessName'").GetOwner().User |
Sort-Object -Unique
And, if you change the name of the process you want to view, you will find other interesting things. The following script will list the users currently connected to your machine through PowerShell remote management:
$ProcessName = ''
try
{
(Get-WmiObject -Query "select * from Win32_Process where name='$ProcessName'").GetOwner().User |
Sort-Object -Unique
}
catch
{
Write-Warning "No user was found."
}