PowerShell view the owning user of the process _powershell

Source: Internet
Author: User

Applies to all Windows PowerShell versions

To see who the owner of a particular process is, and how many instances of a change process, you can try the following code:

Copy Code code as follows:

$ProcessName = ' Explorer.exe '
(get-wmiobject-query "SELECT * from Win32_Process where name= ' $ProcessName ')". GetOwner (). User

Note: There are a number of different ways to get the currently logged in user, but the method varies depending on your operating system version. If you are fine with a graphical user interface, but if the core server is a non user interface, the script may not detect the user who is currently connected to the host.


The example above returns the owner of all the "explorer.exe" processes. If you have administrator privileges and are logged in remotely, the user list may also include inactive users. Because the Explorer.exe that are open on each desktop may belong to different sessions, different users.

If you use the Sort-object command, you can easily exclude duplicates.

Copy Code code as follows:

$ProcessName = ' Explorer.exe '
(get-wmiobject–query "SELECT * from Win32_Process where name= ' $ProcessName ')". GetOwner (). User |
Sort-object-unique

Also, if you change the name of the process you want to view, you'll find other interesting things. The following script lists the users currently connected to your machine through PowerShell remote management:

Copy Code code as follows:

$ProcessName = ' Wsmprovhost.exe '
Try
{
(get-wmiobject-query "SELECT * from Win32_Process where name= ' $ProcessName ')". GetOwner (). User |
Sort-object-unique
}
Catch
{
Write-warning "no user found."
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.