PSVirtualDesktop icon indicating copy to clipboard operation
PSVirtualDesktop copied to clipboard

Get-DesktopFromWindow does not work

Open philipjholm opened this issue 3 years ago • 3 comments

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)``

philipjholm avatar May 14 '22 11:05 philipjholm

Hello @philipjholm,

it seems that for some window handles Get-DesktopFromWindow retrieves non valid desktop guids. I have to explore this further.

Greetings

Markus

MScholtes avatar May 19 '22 15:05 MScholtes

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

MScholtes avatar May 27 '22 06:05 MScholtes

Hello @philipjholm,

the new version 1.5.1 of PSVirtualDesktop should solve the issue.

Greetings

Markus

MScholtes avatar Jul 31 '22 19:07 MScholtes

Closed because there is no reaction

MScholtes avatar Oct 09 '22 13:10 MScholtes