Search code examples
powershellvbscriptautohotkeywindows-11

Windows 11 Cascade Windows: possible with e.g. AutoHotkey, not with VBScript or PowerShell


In Windows 10, I was able to use "Cascade Windows" with a simple .vbs script:

set objShell = CreateObject("shell.application")
call objShell.CascadeWindows()

The same is possible with PowerShell:

$ShellExp = New-Object -ComObject Shell.Application
$ShellExp.CascadeWindows()

However, in Windows 11, this is no longer possible. Nevertheless, the method is present in Windows 11. Issue the following PowerShell command:

New-Object -ComObject "Shell.Application" | gm | select Name, MemberType

And using e.g. AutoHotkey, I am able to "Cascade Windows" using the following script/command:

DllCall( "CascadeWindows", uInt,0, Int,4, Int,0, Int,0, Int,0 )

Is there a way to restore a functioning "Cascade Windows" action using VBScript or PowerShell?


Solution

  • Powershell (with embedded c#):

    $code = @"
    using System;
    using System.Runtime.InteropServices;
    
    namespace Utils
    {
        public class CascadeWindowsApi
        {
            [DllImport("user32.dll")]
            static extern ushort CascadeWindows(IntPtr hwndParent, uint wHow,
            IntPtr lpRect, uint cKids, IntPtr[] lpKids);
    
            public static void Cascade()
            {
                CascadeWindows(IntPtr.Zero, 4, IntPtr.Zero, 0, null);
            }    
        }
    }
    "@
    
    Add-Type -TypeDefinition $code -Language CSharp 
    iex "[Utils.CascadeWindowsApi]::Cascade()"
    

    Works on Windows 11.