Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @(set "0=%~f0"^)#) & powershell -nop -c iex([io.file]::ReadAllText($env:0)) & pause & exit/b
- $host.ui.rawui.windowtitle = "Delete all virtual desktops"
- # RemoveAllVirtualDesktopsRaw function by AveYo
- # All the heavy lifting done by https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5
- # Author: Markus Scholtes, 2017/05/08
- # Version 2.10 - support for Windows 11 incl. Insider, 2021/11/27
- # prefer $PSVersionTable.BuildVersion to [Environment]::OSVersion.Version
- # since a wrong Windows version might be returned in RunSpaces
- if ($PSVersionTable.PSVersion.Major -lt 6)
- { # Powershell 5.x
- $OSVer = $PSVersionTable.BuildVersion.Major
- $OSBuild = $PSVersionTable.BuildVersion.Build
- }
- else
- { # Powershell 6.x or up
- $OSVer = [Environment]::OSVersion.Version.Major
- $OSBuild = [Environment]::OSVersion.Version.Build
- }
- if ($OSVer -lt 10)
- {
- Write-Error "Windows 10 or above is required to run this script"
- exit -1
- }
- if ($OSBuild -lt 14393)
- {
- Write-Error "Windows 10 1607 or above is required to run this script"
- exit -1
- }
- $Windows1607 = $TRUE
- $Windows1803 = $FALSE
- $Windows1809 = $FALSE
- $Windows11 = $FALSE
- $Windows22449 = $FALSE
- if ($OSBuild -ge 17134)
- {
- $Windows1607 = $FALSE
- $Windows1803 = $TRUE
- }
- if ($OSBuild -ge 17661)
- {
- $Windows1607 = $FALSE
- $Windows1803 = $FALSE
- $Windows1809 = $TRUE
- }
- if ($OSBuild -ge 22000)
- {
- $Windows1607 = $FALSE
- $Windows1803 = $FALSE
- $Windows1809 = $FALSE
- $Windows11 = $TRUE
- }
- if ($OSBuild -ge 22449)
- {
- $Windows22449 = $TRUE
- }
- Add-Type -Language CSharp -TypeDefinition @"
- using System;
- using System.Runtime.InteropServices;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Text;
- // Based on http://stackoverflow.com/a/32417530, Windows 10 SDK, github project Grabacr07/VirtualDesktop and own research
- namespace VirtualDesktop
- {
- internal static class Guids
- {
- public static readonly Guid CLSID_ImmersiveShell = new Guid("C2F03A33-21F5-47FA-B4BB-156362A2F239");
- public static readonly Guid CLSID_VirtualDesktopManagerInternal = new Guid("C5E0CDCA-7B6E-41B2-9FC4-D93975CC467B");
- public static readonly Guid CLSID_VirtualDesktopManager = new Guid("AA509086-5CA9-4C25-8F95-589D3C07B48A");
- public static readonly Guid CLSID_VirtualDesktopPinnedApps = new Guid("B5A399E7-1C87-46B8-88E9-FC5747B171BD");
- }
- [StructLayout(LayoutKind.Sequential)]
- internal struct Size
- {
- public int X;
- public int Y;
- }
- [StructLayout(LayoutKind.Sequential)]
- internal struct Rect
- {
- public int Left;
- public int Top;
- public int Right;
- public int Bottom;
- }
- internal enum APPLICATION_VIEW_CLOAK_TYPE : int
- {
- AVCT_NONE = 0,
- AVCT_DEFAULT = 1,
- AVCT_VIRTUAL_DESKTOP = 2
- }
- internal enum APPLICATION_VIEW_COMPATIBILITY_POLICY : int
- {
- AVCP_NONE = 0,
- AVCP_SMALL_SCREEN = 1,
- AVCP_TABLET_SMALL_SCREEN = 2,
- AVCP_VERY_SMALL_SCREEN = 3,
- AVCP_HIGH_SCALE_FACTOR = 4
- }
- [ComImport]
- // https://github.com/mzomparelli/zVirtualDesktop/wiki: Updated interfaces in Windows 10 build 17134, 17661, and 17666
- $(if ($Windows1607) {@"
- // Windows 10 1607 and Server 2016:
- [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
- [Guid("9AC0B5C8-1484-4C5B-9533-4134A0F97CEA")]
- "@ })
- $(if ($Windows1803) {@"
- // Windows 10 1803:
- [InterfaceType(ComInterfaceType.InterfaceIsIInspectable)]
- [Guid("871F602A-2B58-42B4-8C4B-6C43D642C06F")]
- "@ })
- $(if ($Windows1809 -or $Windows11) {@"
- // Windows 10 1809 or up and Windows 11:
- [InterfaceType(ComInterfaceType.InterfaceIsIInspectable)]
- [Guid("372E1D3B-38D3-42E4-A15B-8AB2B178F513")]
- "@ })
- internal interface IApplicationView
- {
- int SetFocus();
- int SwitchTo();
- int TryInvokeBack(IntPtr /* IAsyncCallback* */ callback);
- int GetThumbnailWindow(out IntPtr hwnd);
- int GetMonitor(out IntPtr /* IImmersiveMonitor */ immersiveMonitor);
- int GetVisibility(out int visibility);
- int SetCloak(APPLICATION_VIEW_CLOAK_TYPE cloakType, int unknown);
- int GetPosition(ref Guid guid /* GUID for IApplicationViewPosition */, out IntPtr /* IApplicationViewPosition** */ position);
- int SetPosition(ref IntPtr /* IApplicationViewPosition* */ position);
- int InsertAfterWindow(IntPtr hwnd);
- int GetExtendedFramePosition(out Rect rect);
- int GetAppUserModelId([MarshalAs(UnmanagedType.LPWStr)] out string id);
- int SetAppUserModelId(string id);
- int IsEqualByAppUserModelId(string id, out int result);
- int GetViewState(out uint state);
- int SetViewState(uint state);
- int GetNeediness(out int neediness);
- int GetLastActivationTimestamp(out ulong timestamp);
- int SetLastActivationTimestamp(ulong timestamp);
- int GetVirtualDesktopId(out Guid guid);
- int SetVirtualDesktopId(ref Guid guid);
- int GetShowInSwitchers(out int flag);
- int SetShowInSwitchers(int flag);
- int GetScaleFactor(out int factor);
- int CanReceiveInput(out bool canReceiveInput);
- int GetCompatibilityPolicyType(out APPLICATION_VIEW_COMPATIBILITY_POLICY flags);
- int SetCompatibilityPolicyType(APPLICATION_VIEW_COMPATIBILITY_POLICY flags);
- $(if ($Windows1607) {@"
- int GetPositionPriority(out IntPtr /* IShellPositionerPriority** */ priority);
- int SetPositionPriority(IntPtr /* IShellPositionerPriority* */ priority);
- "@ })
- int GetSizeConstraints(IntPtr /* IImmersiveMonitor* */ monitor, out Size size1, out Size size2);
- int GetSizeConstraintsForDpi(uint uint1, out Size size1, out Size size2);
- int SetSizeConstraintsForDpi(ref uint uint1, ref Size size1, ref Size size2);
- $(if ($Windows1607) {@"
- int QuerySizeConstraintsFromApp();
- "@ })
- int OnMinSizePreferencesUpdated(IntPtr hwnd);
- int ApplyOperation(IntPtr /* IApplicationViewOperation* */ operation);
- int IsTray(out bool isTray);
- int IsInHighZOrderBand(out bool isInHighZOrderBand);
- int IsSplashScreenPresented(out bool isSplashScreenPresented);
- int Flash();
- int GetRootSwitchableOwner(out IApplicationView rootSwitchableOwner);
- int EnumerateOwnershipTree(out IObjectArray ownershipTree);
- int GetEnterpriseId([MarshalAs(UnmanagedType.LPWStr)] out string enterpriseId);
- int IsMirrored(out bool isMirrored);
- $(if ($Windows1803) {@"
- int Unknown1(out int unknown);
- int Unknown2(out int unknown);
- int Unknown3(out int unknown);
- int Unknown4(out int unknown);
- "@ })
- $(if ($Windows1809 -or $Windows11) {@"
- int Unknown1(out int unknown);
- int Unknown2(out int unknown);
- int Unknown3(out int unknown);
- int Unknown4(out int unknown);
- int Unknown5(out int unknown);
- int Unknown6(int unknown);
- int Unknown7();
- int Unknown8(out int unknown);
- int Unknown9(int unknown);
- int Unknown10(int unknownX, int unknownY);
- int Unknown11(int unknown);
- int Unknown12(out Size size1);
- "@ })
- }
- [ComImport]
- [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
- $(if ($Windows1607) {@"
- // Windows 10 1607 and Server 2016:
- [Guid("2C08ADF0-A386-4B35-9250-0FE183476FCC")]
- "@ })
- $(if ($Windows1803) {@"
- // Windows 10 1803:
- [Guid("2C08ADF0-A386-4B35-9250-0FE183476FCC")]
- "@ })
- $(if ($Windows1809 -or $Windows11) {@"
- // Windows 10 1809 or up and Windows 11:
- [Guid("1841C6D7-4F9D-42C0-AF41-8747538F10E5")]
- "@ })
- internal interface IApplicationViewCollection
- {
- int GetViews(out IObjectArray array);
- int GetViewsByZOrder(out IObjectArray array);
- int GetViewsByAppUserModelId(string id, out IObjectArray array);
- int GetViewForHwnd(IntPtr hwnd, out IApplicationView view);
- int GetViewForApplication(object application, out IApplicationView view);
- int GetViewForAppUserModelId(string id, out IApplicationView view);
- int GetViewInFocus(out IntPtr view);
- $(if ($Windows1803 -or $Windows1809 -or $Windows11) {@"
- // Windows 10 1803 or up and Windows 11:
- int Unknown1(out IntPtr view);
- "@ })
- void RefreshCollection();
- int RegisterForApplicationViewChanges(object listener, out int cookie);
- $(if ($Windows1607) {@"
- // Windows 10 1607 and Server 2016:
- int RegisterForApplicationViewPositionChanges(object listener, out int cookie);
- "@ })
- $(if ($Windows1803) {@"
- // Windows 10 1803:
- int RegisterForApplicationViewPositionChanges(object listener, out int cookie);
- "@ })
- int UnregisterForApplicationViewChanges(int cookie);
- }
- [ComImport]
- [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
- $(if ($Windows11) {@"
- // Windows 11:
- [Guid("536D3495-B208-4CC9-AE26-DE8111275BF8")]
- "@ } else {@"
- // Windows 10:
- [Guid("FF72FFDD-BE7E-43FC-9C03-AD81681E88E4")]
- "@ })
- internal interface IVirtualDesktop
- {
- bool IsViewVisible(IApplicationView view);
- Guid GetId();
- $(if ($Windows11) {@"
- IntPtr Unknown1();
- [return: MarshalAs(UnmanagedType.HString)]
- string GetName();
- [return: MarshalAs(UnmanagedType.HString)]
- string GetWallpaperPath();
- "@ })
- }
- [ComImport]
- [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
- $(if ($Windows11) {@"
- // Windows 11:
- [Guid("B2F925B9-5A0F-4D2E-9F4D-2B1507593C10")]
- internal interface IVirtualDesktopManagerInternal
- {
- int GetCount(IntPtr hWndOrMon);
- void MoveViewToDesktop(IApplicationView view, IVirtualDesktop desktop);
- bool CanViewMoveDesktops(IApplicationView view);
- IVirtualDesktop GetCurrentDesktop(IntPtr hWndOrMon);
- "@ })
- $(if ($Windows22449) {@"
- // Windows 11 Insider:
- IObjectArray GetAllCurrentDesktops();
- "@ })
- $(if ($Windows11) {@"
- void GetDesktops(IntPtr hWndOrMon, out IObjectArray desktops);
- [PreserveSig]
- int GetAdjacentDesktop(IVirtualDesktop from, int direction, out IVirtualDesktop desktop);
- void SwitchDesktop(IntPtr hWndOrMon, IVirtualDesktop desktop);
- IVirtualDesktop CreateDesktop(IntPtr hWndOrMon);
- void MoveDesktop(IVirtualDesktop desktop, IntPtr hWndOrMon, int nIndex);
- void RemoveDesktop(IVirtualDesktop desktop, IVirtualDesktop fallback);
- IVirtualDesktop FindDesktop(ref Guid desktopid);
- void GetDesktopSwitchIncludeExcludeViews(IVirtualDesktop desktop, out IObjectArray unknown1, out IObjectArray unknown2);
- void SetDesktopName(IVirtualDesktop desktop, [MarshalAs(UnmanagedType.HString)] string name);
- void SetDesktopWallpaper(IVirtualDesktop desktop, [MarshalAs(UnmanagedType.HString)] string path);
- void UpdateWallpaperPathForAllDesktops([MarshalAs(UnmanagedType.HString)] string path);
- void CopyDesktopState(IApplicationView pView0, IApplicationView pView1);
- int GetDesktopIsPerMonitor();
- void SetDesktopIsPerMonitor(bool state);
- }
- "@ } else {@"
- // Windows 10:
- [Guid("F31574D6-B682-4CDC-BD56-1827860ABEC6")]
- internal interface IVirtualDesktopManagerInternal
- {
- int GetCount();
- void MoveViewToDesktop(IApplicationView view, IVirtualDesktop desktop);
- bool CanViewMoveDesktops(IApplicationView view);
- IVirtualDesktop GetCurrentDesktop();
- void GetDesktops(out IObjectArray desktops);
- [PreserveSig]
- int GetAdjacentDesktop(IVirtualDesktop from, int direction, out IVirtualDesktop desktop);
- void SwitchDesktop(IVirtualDesktop desktop);
- IVirtualDesktop CreateDesktop();
- void RemoveDesktop(IVirtualDesktop desktop, IVirtualDesktop fallback);
- IVirtualDesktop FindDesktop(ref Guid desktopid);
- }
- [ComImport]
- [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
- [Guid("0F3A72B0-4566-487E-9A33-4ED302F6D6CE")]
- internal interface IVirtualDesktopManagerInternal2
- {
- int GetCount();
- void MoveViewToDesktop(IApplicationView view, IVirtualDesktop desktop);
- bool CanViewMoveDesktops(IApplicationView view);
- IVirtualDesktop GetCurrentDesktop();
- void GetDesktops(out IObjectArray desktops);
- [PreserveSig]
- int GetAdjacentDesktop(IVirtualDesktop from, int direction, out IVirtualDesktop desktop);
- void SwitchDesktop(IVirtualDesktop desktop);
- IVirtualDesktop CreateDesktop();
- void RemoveDesktop(IVirtualDesktop desktop, IVirtualDesktop fallback);
- IVirtualDesktop FindDesktop(ref Guid desktopid);
- void Unknown1(IVirtualDesktop desktop, out IntPtr unknown1, out IntPtr unknown2);
- void SetName(IVirtualDesktop desktop, [MarshalAs(UnmanagedType.HString)] string name);
- }
- "@ })
- [ComImport]
- [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
- [Guid("A5CD92FF-29BE-454C-8D04-D82879FB3F1B")]
- internal interface IVirtualDesktopManager
- {
- bool IsWindowOnCurrentVirtualDesktop(IntPtr topLevelWindow);
- Guid GetWindowDesktopId(IntPtr topLevelWindow);
- void MoveWindowToDesktop(IntPtr topLevelWindow, ref Guid desktopId);
- }
- [ComImport]
- [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
- [Guid("4CE81583-1E4C-4632-A621-07A53543148F")]
- internal interface IVirtualDesktopPinnedApps
- {
- bool IsAppIdPinned(string appId);
- void PinAppID(string appId);
- void UnpinAppID(string appId);
- bool IsViewPinned(IApplicationView applicationView);
- void PinView(IApplicationView applicationView);
- void UnpinView(IApplicationView applicationView);
- }
- [ComImport]
- [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
- [Guid("92CA9DCD-5622-4BBA-A805-5E9F541BD8C9")]
- internal interface IObjectArray
- {
- void GetCount(out int count);
- void GetAt(int index, ref Guid iid, [MarshalAs(UnmanagedType.Interface)]out object obj);
- }
- [ComImport]
- [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
- [Guid("6D5140C1-7436-11CE-8034-00AA006009FA")]
- internal interface IServiceProvider10
- {
- [return: MarshalAs(UnmanagedType.IUnknown)]
- object QueryService(ref Guid service, ref Guid riid);
- }
- internal static class DesktopManager
- {
- static DesktopManager()
- {
- var shell = (IServiceProvider10)Activator.CreateInstance(Type.GetTypeFromCLSID(Guids.CLSID_ImmersiveShell));
- VirtualDesktopManagerInternal = (IVirtualDesktopManagerInternal)shell.QueryService(Guids.CLSID_VirtualDesktopManagerInternal, typeof(IVirtualDesktopManagerInternal).GUID);
- $(if (-not $Windows11) {@"
- // Windows 10:
- try {
- VirtualDesktopManagerInternal2 = (IVirtualDesktopManagerInternal2)shell.QueryService(Guids.CLSID_VirtualDesktopManagerInternal, typeof(IVirtualDesktopManagerInternal2).GUID);
- }
- catch {
- VirtualDesktopManagerInternal2 = null;
- }
- "@ })
- VirtualDesktopManager = (IVirtualDesktopManager)Activator.CreateInstance(Type.GetTypeFromCLSID(Guids.CLSID_VirtualDesktopManager));
- ApplicationViewCollection = (IApplicationViewCollection)shell.QueryService(typeof(IApplicationViewCollection).GUID, typeof(IApplicationViewCollection).GUID);
- VirtualDesktopPinnedApps = (IVirtualDesktopPinnedApps)shell.QueryService(Guids.CLSID_VirtualDesktopPinnedApps, typeof(IVirtualDesktopPinnedApps).GUID);
- }
- internal static IVirtualDesktopManagerInternal VirtualDesktopManagerInternal;
- $(if (-not $Windows11) {@"
- // Windows 10:
- internal static IVirtualDesktopManagerInternal2 VirtualDesktopManagerInternal2;
- "@ })
- internal static IVirtualDesktopManager VirtualDesktopManager;
- internal static IApplicationViewCollection ApplicationViewCollection;
- internal static IVirtualDesktopPinnedApps VirtualDesktopPinnedApps;
- internal static IVirtualDesktop GetDesktop(int index)
- { // get desktop with index
- $(if (-not $Windows11) {@"
- int count = VirtualDesktopManagerInternal.GetCount();
- "@ } else {@"
- int count = VirtualDesktopManagerInternal.GetCount(IntPtr.Zero);
- "@ })
- if (index < 0 || index >= count) throw new ArgumentOutOfRangeException("index");
- IObjectArray desktops;
- $(if (-not $Windows11) {@"
- VirtualDesktopManagerInternal.GetDesktops(out desktops);
- "@ } else {@"
- VirtualDesktopManagerInternal.GetDesktops(IntPtr.Zero, out desktops);
- "@ })
- object objdesktop;
- desktops.GetAt(index, typeof(IVirtualDesktop).GUID, out objdesktop);
- Marshal.ReleaseComObject(desktops);
- return (IVirtualDesktop)objdesktop;
- }
- internal static int GetDesktopIndex(IVirtualDesktop desktop)
- { // get index of desktop
- int index = -1;
- Guid IdSearch = desktop.GetId();
- IObjectArray desktops;
- $(if (-not $Windows11) {@"
- VirtualDesktopManagerInternal.GetDesktops(out desktops);
- "@ } else {@"
- VirtualDesktopManagerInternal.GetDesktops(IntPtr.Zero, out desktops);
- "@ })
- object objdesktop;
- $(if (-not $Windows11) {@"
- for (int i = 0; i < VirtualDesktopManagerInternal.GetCount(); i++)
- "@ } else {@"
- for (int i = 0; i < VirtualDesktopManagerInternal.GetCount(IntPtr.Zero); i++)
- "@ })
- {
- desktops.GetAt(i, typeof(IVirtualDesktop).GUID, out objdesktop);
- if (IdSearch.CompareTo(((IVirtualDesktop)objdesktop).GetId()) == 0)
- { index = i;
- break;
- }
- }
- Marshal.ReleaseComObject(desktops);
- return index;
- }
- internal static IApplicationView GetApplicationView(this IntPtr hWnd)
- { // get application view to window handle
- IApplicationView view;
- ApplicationViewCollection.GetViewForHwnd(hWnd, out view);
- return view;
- }
- internal static string GetAppId(IntPtr hWnd)
- { // get Application ID to window handle
- string appId;
- hWnd.GetApplicationView().GetAppUserModelId(out appId);
- return appId;
- }
- }
- public class Desktop
- {
- // Get window handle of current console window (even if powershell started in cmd)
- [DllImport("Kernel32.dll")]
- public static extern IntPtr GetConsoleWindow();
- // get process id to window handle
- [DllImport("user32.dll")]
- private static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
- // get handle of active window
- [DllImport("user32.dll")]
- public static extern IntPtr GetForegroundWindow();
- private IVirtualDesktop ivd;
- private Desktop(IVirtualDesktop desktop) { this.ivd = desktop; }
- public override int GetHashCode()
- { // Get hash
- return ivd.GetHashCode();
- }
- public override bool Equals(object obj)
- { // Compares with object
- var desk = obj as Desktop;
- return desk != null && object.ReferenceEquals(this.ivd, desk.ivd);
- }
- public static int Count
- { // return the number of desktops
- $(if (-not $Windows11) {@"
- get { return DesktopManager.VirtualDesktopManagerInternal.GetCount(); }
- "@ } else {@"
- get { return DesktopManager.VirtualDesktopManagerInternal.GetCount(IntPtr.Zero); }
- "@ })
- }
- public static Desktop Current
- { // returns current desktop
- $(if (-not $Windows11) {@"
- get { return new Desktop(DesktopManager.VirtualDesktopManagerInternal.GetCurrentDesktop()); }
- "@ } else {@"
- get { return new Desktop(DesktopManager.VirtualDesktopManagerInternal.GetCurrentDesktop(IntPtr.Zero)); }
- "@ })
- }
- public static Desktop FromIndex(int index)
- { // return desktop object from index (-> index = 0..Count-1)
- return new Desktop(DesktopManager.GetDesktop(index));
- }
- public static Desktop FromWindow(IntPtr hWnd)
- { // return desktop object to desktop on which window <hWnd> is displayed
- if (hWnd == IntPtr.Zero) throw new ArgumentNullException();
- Guid id = DesktopManager.VirtualDesktopManager.GetWindowDesktopId(hWnd);
- return new Desktop(DesktopManager.VirtualDesktopManagerInternal.FindDesktop(ref id));
- }
- public static int FromDesktop(Desktop desktop)
- { // return index of desktop object or -1 if not found
- return DesktopManager.GetDesktopIndex(desktop.ivd);
- }
- public static Desktop Create()
- { // create a new desktop
- $(if (-not $Windows11) {@"
- return new Desktop(DesktopManager.VirtualDesktopManagerInternal.CreateDesktop());
- "@ } else {@"
- return new Desktop(DesktopManager.VirtualDesktopManagerInternal.CreateDesktop(IntPtr.Zero));
- "@ })
- }
- public void Remove(Desktop fallback = null)
- { // destroy desktop and switch to <fallback>
- IVirtualDesktop fallbackdesktop;
- if (fallback == null)
- { // if no fallback is given use desktop to the left except for desktop 0.
- Desktop dtToCheck = new Desktop(DesktopManager.GetDesktop(0));
- if (this.Equals(dtToCheck))
- { // desktop 0: set fallback to second desktop (= "right" desktop)
- DesktopManager.VirtualDesktopManagerInternal.GetAdjacentDesktop(ivd, 4, out fallbackdesktop); // 4 = RightDirection
- }
- else
- { // set fallback to "left" desktop
- DesktopManager.VirtualDesktopManagerInternal.GetAdjacentDesktop(ivd, 3, out fallbackdesktop); // 3 = LeftDirection
- }
- }
- else
- // set fallback desktop
- fallbackdesktop = fallback.ivd;
- DesktopManager.VirtualDesktopManagerInternal.RemoveDesktop(ivd, fallbackdesktop);
- }
- public bool IsVisible
- { // return true if this desktop is the current displayed one
- $(if (-not $Windows11) {@"
- get { return object.ReferenceEquals(ivd, DesktopManager.VirtualDesktopManagerInternal.GetCurrentDesktop()); }
- "@ } else {@"
- get { return object.ReferenceEquals(ivd, DesktopManager.VirtualDesktopManagerInternal.GetCurrentDesktop(IntPtr.Zero)); }
- "@ })
- }
- public void MakeVisible()
- { // make this desktop visible
- $(if (-not $Windows11) {@"
- DesktopManager.VirtualDesktopManagerInternal.SwitchDesktop(ivd);
- "@ } else {@"
- DesktopManager.VirtualDesktopManagerInternal.SwitchDesktop(IntPtr.Zero, ivd);
- "@ })
- }
- public Desktop Left
- { // return desktop at the left of this one, null if none
- get
- {
- IVirtualDesktop desktop;
- int hr = DesktopManager.VirtualDesktopManagerInternal.GetAdjacentDesktop(ivd, 3, out desktop); // 3 = LeftDirection
- if (hr == 0)
- return new Desktop(desktop);
- else
- return null;
- }
- }
- public Desktop Right
- { // return desktop at the right of this one, null if none
- get
- {
- IVirtualDesktop desktop;
- int hr = DesktopManager.VirtualDesktopManagerInternal.GetAdjacentDesktop(ivd, 4, out desktop); // 4 = RightDirection
- if (hr == 0)
- return new Desktop(desktop);
- else
- return null;
- }
- }
- public void MoveWindow(IntPtr hWnd)
- { // move window to this desktop
- int processId;
- if (hWnd == IntPtr.Zero) throw new ArgumentNullException();
- GetWindowThreadProcessId(hWnd, out processId);
- if (hWnd == GetConsoleWindow())
- { // own window
- try // the easy way (powershell's own console)
- {
- DesktopManager.VirtualDesktopManager.MoveWindowToDesktop(hWnd, ivd.GetId());
- }
- catch // powershell in cmd console
- {
- IApplicationView view;
- DesktopManager.ApplicationViewCollection.GetViewForHwnd(hWnd, out view);
- DesktopManager.VirtualDesktopManagerInternal.MoveViewToDesktop(view, ivd);
- }
- }
- else
- { // window of other process
- IApplicationView view;
- DesktopManager.ApplicationViewCollection.GetViewForHwnd(hWnd, out view);
- try {
- DesktopManager.VirtualDesktopManagerInternal.MoveViewToDesktop(view, ivd);
- }
- catch
- { // could not move active window, try main window (or whatever windows thinks is the main window)
- DesktopManager.ApplicationViewCollection.GetViewForHwnd(System.Diagnostics.Process.GetProcessById(processId).MainWindowHandle, out view);
- DesktopManager.VirtualDesktopManagerInternal.MoveViewToDesktop(view, ivd);
- }
- }
- }
- public void MoveActiveWindow()
- { // move active window to this desktop
- MoveWindow(GetForegroundWindow());
- }
- public bool HasWindow(IntPtr hWnd)
- { // return true if window is on this desktop
- if (hWnd == IntPtr.Zero) throw new ArgumentNullException();
- return ivd.GetId() == DesktopManager.VirtualDesktopManager.GetWindowDesktopId(hWnd);
- }
- public static bool IsWindowPinned(IntPtr hWnd)
- { // return true if window is pinned to all desktops
- if (hWnd == IntPtr.Zero) throw new ArgumentNullException();
- return DesktopManager.VirtualDesktopPinnedApps.IsViewPinned(hWnd.GetApplicationView());
- }
- public static void PinWindow(IntPtr hWnd)
- { // pin window to all desktops
- if (hWnd == IntPtr.Zero) throw new ArgumentNullException();
- var view = hWnd.GetApplicationView();
- if (!DesktopManager.VirtualDesktopPinnedApps.IsViewPinned(view))
- { // pin only if not already pinned
- DesktopManager.VirtualDesktopPinnedApps.PinView(view);
- }
- }
- public static void UnpinWindow(IntPtr hWnd)
- { // unpin window from all desktops
- if (hWnd == IntPtr.Zero) throw new ArgumentNullException();
- var view = hWnd.GetApplicationView();
- if (DesktopManager.VirtualDesktopPinnedApps.IsViewPinned(view))
- { // unpin only if not already unpinned
- DesktopManager.VirtualDesktopPinnedApps.UnpinView(view);
- }
- }
- public static bool IsApplicationPinned(IntPtr hWnd)
- { // return true if application for window is pinned to all desktops
- if (hWnd == IntPtr.Zero) throw new ArgumentNullException();
- return DesktopManager.VirtualDesktopPinnedApps.IsAppIdPinned(DesktopManager.GetAppId(hWnd));
- }
- public static void PinApplication(IntPtr hWnd)
- { // pin application for window to all desktops
- if (hWnd == IntPtr.Zero) throw new ArgumentNullException();
- string appId = DesktopManager.GetAppId(hWnd);
- if (!DesktopManager.VirtualDesktopPinnedApps.IsAppIdPinned(appId))
- { // pin only if not already pinned
- DesktopManager.VirtualDesktopPinnedApps.PinAppID(appId);
- }
- }
- public static void UnpinApplication(IntPtr hWnd)
- { // unpin application for window from all desktops
- if (hWnd == IntPtr.Zero) throw new ArgumentNullException();
- var view = hWnd.GetApplicationView();
- string appId = DesktopManager.GetAppId(hWnd);
- if (DesktopManager.VirtualDesktopPinnedApps.IsAppIdPinned(appId))
- { // unpin only if pinned
- DesktopManager.VirtualDesktopPinnedApps.UnpinAppID(appId);
- }
- }
- }
- }
- "@
- Function RemoveAllVirtualDesktopsRaw
- {
- $VDCount=([VirtualDesktop.Desktop]::Count)-1;
- write-host number of virtual desktops: 0 - $VDCount;
- $VDCurrent=([VirtualDesktop.Desktop]::Current);
- $VDIndex=[VirtualDesktop.Desktop]::FromDesktop(([VirtualDesktop.Desktop]::Current));
- write-host current virtual desktop: $VDIndex;
- $ErrorActionPreference='SilentlyContinue';
- For ($i=$VDCount; $i -ge 0; $i--) {
- # if ($i -eq $VDIndex) {
- # write-host - skipping $i... [current];
- # continue;
- # }
- # write-host - removing $i...;
- $VDTarget = [VirtualDesktop.Desktop]::FromIndex($i);
- # if ($VDTarget -is [VirtualDesktop.Desktop])
- # {
- $VDTarget.Remove($VDCurrent);
- # }
- }
- }
- $started=(GET-DATE);
- RemoveAllVirtualDesktopsRaw
- $finished=(NEW-TIMESPAN $started);
- write-host "done in $($finished.Seconds).$($finished.MilliSeconds) seconds"
Add Comment
Please, Sign In to add comment