Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Get-WmiObject -Query "SELECT * FROM Win32_Process WHERE Name = 'dwm.exe'" | ForEach-Object {
- $owner = $_.GetOwner()
- $username = $owner.User
- $domain = $owner.Domain
- Write-Output "Process ID: $($_.ProcessId), User: $domain\$username"
- }
- try two:
- # Get all dwm.exe processes
- $dwms = Get-Process -Name dwm -ErrorAction SilentlyContinue
- # Loop through the dwm.exe processes
- foreach ($dwm in $dwms) {
- $processId = $dwm.Id
- $owner = $null
- # Try to get the owner of the process using Get-WmiObject
- $process = Get-WmiObject -Class Win32_Process | Where-Object { $_.ProcessId -eq $processId } -ErrorAction SilentlyContinue
- if ($process) {
- $owner = $process.GetOwner()
- }
- # If Get-WmiObject failed or didn't provide the owner, try using Get-Process
- if (-not $owner) {
- $owner = $dwm | Select-Object -ExpandProperty UserName
- }
- Write-Output "Process ID: $processId, User: $owner"
- }
- try three:
- # Get all dwm.exe processes
- $dwms = Get-Process -Name dwm -ErrorAction SilentlyContinue
- # Loop through the dwm.exe processes
- foreach ($dwm in $dwms) {
- $processId = $dwm.Id
- $owner = $null
- # Try to get the owner of the process using Get-WmiObject
- $process = Get-WmiObject -Class Win32_Process | Where-Object { $_.ProcessId -eq $processId } -ErrorAction SilentlyContinue
- if ($process) {
- $owner = $process.GetOwner() | Select-Object -ExpandProperty User
- }
- # If Get-WmiObject failed or didn't provide the owner, try using Get-Process
- if (-not $owner) {
- $owner = ($dwm | Get-Process | Select-Object -ExpandProperty UserName) -replace ".*\\"
- }
- Write-Output "Process ID: $processId, User: $owner"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement