PSVirtualDesktop
PSVirtualDesktop copied to clipboard
Get-DesktopFromWindow does not work
I am on Windows 11 and I can't seem to get the Get-DesktopFromWindow function to work. It always gives me the value "VirtualDesktop.Desktop even though my notepad window is placed on the second virtual screen (called Playnite). Am I doing something wrong? See my code below.
$OldDesktop = Get-CurrentDesktop $OldDesktop | Set-DesktopName -Name "Desktop" $NewDesktop = New-Desktop $NewDesktop | Set-DesktopName -Name "Playnite"
Add-Type -AssemblyName PresentationCore,PresentationFramework $msgBody = Get-DesktopFromWindow ((Get-Process "notepad")[0].MainWindowHandle) [System.Windows.MessageBox]::Show($msgBody)``
Hello @philipjholm,
it seems that for some window handles Get-DesktopFromWindow retrieves non valid desktop guids. I have to explore this further.
Greetings
Markus
Hello @philipjholm,
it took some time to find the issue. The error appears only for Windows that are pinned to all desktop and this is logical in its way: a pinned window has no single desktop.
I will correct this in the next version of PSVirtualDesktop. Until then you might use a workaround like this:
$Handle = (Get-Process "notepad")[0].MainWindowHandle
if ((Test-WindowPinned -Hwnd $Handle) -Or (Test-ApplicationPinned -Hwnd $Handle))
{
"Window is pinned"
Get-CurrentDesktop | Get-DesktopName
} else {
"Window is not pinned"
Get-DesktopFromWindow -Hwnd $Handle | Get-DesktopName
}
Greetings
Markus
Hello @philipjholm,
the new version 1.5.1 of PSVirtualDesktop should solve the issue.
Greetings
Markus
Closed because there is no reaction