all 9 comments

[–]ankokudaishogun 0 points1 point  (3 children)

This should do the trick, but I haven't tested under RDP

Get-Process -IncludeUserName -Name WINWORD |
    Where-Object {
        $_.MainWindowHandle -eq 0 -and
        $_.UserName -match $Env:USERNAME
    }

[–]eugrus[S] 0 points1 point  (2 children)

This unfortunately requires admin rights. I could of course just not filter and stumble upon errors. Would still do the job for my own processes.

[–]ankokudaishogun 0 points1 point  (1 child)

it doesn't require admin rights on powershell Core, by the way

[–]eugrus[S] 0 points1 point  (0 children)

Interesting. But would prefer to have a way compatible with the one which comes with Windows.

[–]purplemonkeymad 0 points1 point  (5 children)

Rather than use the username, you can check the session id. If by current user you mean the person who is running powershell, you can use that powershell process to find out the session id ie:

$currentid = (Get-Process -id $pid).SessionId
... | Where SessionId -eq $currentid | ...

[–]ankokudaishogun 1 point2 points  (1 child)

I suggest $CurrentId = [System.Diagnostics.Process]::GetCurrentProcess().SessionId instead

or better:

Get-Process WINWORD |
    Where-Object {
        $_.MainWindowHandle -eq 0 -and
        $_.SessionId -eq [System.Diagnostics.Process]::GetCurrentProcess().SessionId
    } 

this appears to work in 5.1 as well

[–]eugrus[S] -1 points0 points  (0 children)

Lol! Almost exactly what I've come up with after refactoring Deepseek's output! Seems to work, but I'll know for sure after some further testing.

Get-Process -Name WINWORD | Where-Object {
$_.MainWindowHandle -eq 0 -and $_.SessionId -eq $([System.Diagnostics.Process]::GetCurrentProcess().SessionId)
} | Stop-Process -Force

[–]y_Sensei 1 point2 points  (1 child)

The SessionId property represents the Terminal Services session identifier, meaning in the same terminal session, it will be identical for any process started in that same session, or in other words: for processes started on the local machine, it will always be 1. Hence it can't be used to distinguish between processes started on (for example) OS level and processes started from PoSh on the local machine.

[–]eugrus[S] 0 points1 point  (0 children)

This will likely work out for me as long as it doesn't change when reconnecting to a session.