Functions/GenXdev.Windows/Get-Window.ps1

################################################################################
<#
.SYNOPSIS
Gets window information for specified processes or window handles.
 
.DESCRIPTION
Retrieves window information using process name, ID, or window handle. Returns
WindowObj objects containing details about the main windows of matching processes.
Supports wildcards when searching by process name.
 
.PARAMETER ProcessName
Name of the process(es) to get window information for. Supports wildcards.
 
.PARAMETER ProcessId
Process ID to get window information for. Must be a valid running process ID.
 
.PARAMETER WindowHandle
Window handle to get information for. Must be a valid window handle number.
 
.EXAMPLE
Get-Window -ProcessName "notepad"
Gets window information for all running Notepad instances.
 
.EXAMPLE
gwin -Id 1234
Gets window information for the process with ID 1234 using the alias.
 
.EXAMPLE
window -Handle 45678
Gets window information for specific window handle using the alias.
#>

function Get-Window {

    [CmdletBinding(DefaultParameterSetName = "ByProcessName")]
    [Alias("gwin", "window")]
    param (
        ########################################################################
        [Parameter(
            Mandatory = $true,
            Position = 0,
            ParameterSetName = "ByProcessName",
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = "Name of the process to get window information for"
        )]
        [ValidateNotNullOrEmpty()]
        [Alias("Name")]
        [string] $ProcessName,

        ########################################################################
        [Parameter(
            Mandatory = $true,
            Position = 0,
            ParameterSetName = "ByProcessId",
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = "ID of the process to get window information for"
        )]
        [ValidateNotNull()]
        [Alias("Id", "PID")]
        [int] $ProcessId,

        ########################################################################
        [Parameter(
            Mandatory = $true,
            Position = 0,
            ParameterSetName = "ByWindowHandle",
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = "Window handle to get information for"
        )]
        [ValidateNotNull()]
        [Alias("Handle", "hWnd")]
        [long] $WindowHandle
        ########################################################################
    )

    begin {

        Write-Verbose "Starting Get-Window with ParameterSet: $($PSCmdlet.ParameterSetName)"
    }

    process {

        # if window handle provided, get window info directly
        if ($WindowHandle -gt 0) {

            Write-Verbose "Getting window information for handle: $WindowHandle"
            [GenXdev.Helpers.WindowObj]::GetMainWindow($WindowHandle)
            return
        }

        # if process id provided, get window info for that specific process
        if ($ProcessId -gt 0) {

            Write-Verbose "Getting window information for process ID: $ProcessId"
            $process = Get-Process -Id $ProcessId -ErrorAction SilentlyContinue

            if ($null -ne $process -and $process.MainWindowHandle -ne 0) {
                [GenXdev.Helpers.WindowObj]::GetMainWindow($process)
            }
            return
        }

        # get window info for all processes matching the name pattern
        Write-Verbose "Getting window information for process name: $ProcessName"
        Get-Process "*$ProcessName*" -ErrorAction SilentlyContinue |
        Where-Object { $_.MainWindowHandle -ne 0 } |
        ForEach-Object {
            [GenXdev.Helpers.WindowObj]::GetMainWindow($_)
        }
    }

    end {
    }
}
################################################################################