Juan Antonio Tubío
Juan Antonio Tubío

Reputation: 1191

Closing all Brave Profile Windows on a Virtual Desktop using Power Shell

I have multiple Brave profiles opened on Windows. I'm using virtual desktops and I have 4 profiles that I always open on the 'Developer' desktop. Let's say profiles number 2 to 6.

I have create a PowerShell script that creates the 'Developer' desktop and open these profiles on it.

Now, I want to create another script that allows me to remove that desktop and close these profiles windows.

I'm using PSVirtualDesktop module to manage Virtual Desktops. (https://github.com/MScholtes/PSVirtualDesktop)

I tried to get all opened windows and iterate on them using Get-DesktopFromWindow or Test-Window to filter them. But I can't get the Brave Profile's windows hwnd.

I tried to use EnumChildWindows, EnumWindows from user32.dll but didn't got any open windows list.

Is there any way to get all opened windows by Brave so I can iterate them?

I tried this code and got nothing on [User32]::EnumWindows($enumWindowsProc, [IntPtr]::Zero) either $windowHandles

# Cargar el módulo PInvoke
Import-Module PInvoke

# Define the user32.dll functions we need
Add-Type @"
    using System;
    using System.Runtime.InteropServices;

    public class User32 {
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll", SetLastError = true)]
        public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

        public delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam);
    }
"@

# Lista de manejadores de ventana en PowerShell
$windowHandles = @()

# Define la función para enumerar las ventanas
$enumWindowsProc = [User32+EnumWindowsProc]{
    param (
        [IntPtr]$hWnd,
        [IntPtr]$lParam
    )

    # Obtener el ID del proceso asociado a la ventana
    [UInt32]$processId = 0
    [User32]::GetWindowThreadProcessId($hWnd, [ref]$processId)

    # Comprobar si el proceso corresponde a Brave
    if ($processId -ne 0) {
        $process = Get-Process -Id $processId -ErrorAction SilentlyContinue
        if ($process -and $process.ProcessName -eq "brave") {
            $global:windowHandles += $hWnd
        }
    }

    return $true
}

# Enumerar todas las ventanas
[User32]::EnumWindows($enumWindowsProc, [IntPtr]::Zero)

# Mostrar los manejadores de ventana encontrados
$windowHandles

Also, I found this:

        // get list of all windows with title
        public static List<WindowInformation> GetWindows()
        {
            WindowInformationList = new List<WindowInformation>();
            EnumWindows(callBackPtr, IntPtr.Zero);
            return WindowInformationList;
        }

On VirtualDesktop code, buy I don't know how to use it: https://github.com/MScholtes/PSVirtualDesktop/blob/master/Module/VirtualDesktop.ps1#L1134

Upvotes: 0

Views: 26

Answers (0)

Related Questions