PSTerminal.psm1

<#
.SYNOPSIS
Aliases and functions for managing the default terminal in Windows.
 
.DESCRIPTION
This script defines aliases and functions to get and set the default terminal in Windows. It includes the following functionalities:
 
1. Get-Terminal: Retrieves the current terminal settings.
2. Set-Terminal: Sets the default terminal based on user input or predefined options.
 
The script uses registry settings to manage the terminal options.
 
#>


Set-Alias -Name gt -Value Get-Terminal
Set-Alias -Name st -Value Set-Terminal

function Get-Terminal 
{
    <#
    .SYNOPSIS
    Retrieves the current terminal settings.
 
    .DESCRIPTION
    The Get-Terminal function fetches the current terminal settings from the registry.
    It can return all available terminal options or the default terminal based on the provided parameters.
 
    .PARAMETER default
    Retrieves the default terminal settings.
 
    .PARAMETER d
    An alias for the default parameter.
 
    .EXAMPLE
    Get-Terminal
    Retrieves all terminal settings.
 
    .EXAMPLE
    Get-Terminal -default
    Retrieves the default terminal setting.
 
    .NOTES
    Author: Your Name
    #>

    
    param(
        [switch]$default,
        [switch]$d
    )

    $regP = "HKCU:\Console\%%Startup"
    $Cons = 'Let Windows Dedicate', 'Windows Console Host', 'Windows Terminal'
    $DCs  = "{00000000-0000-0000-0000-000000000000}", 
           "{B23D10C0-E52E-411E-9D5B-C09FDF709C7D}",
           "{2EACA947-7F5F-4CFA-BA87-8F7FBEEFBE69}"
    $DTs  = $DCs[0], $DCs[1], "{E12CFF52-A866-4C77-9A90-F570A7AA2C6B}"
    $Names = (Get-Item -Path $regP).Property
    $Guids = $Names | ForEach-Object { Get-ItemPropertyValue -Path $regP -Name $_ }
    $Terminal = @()
    for ($i = 0; $i -lt $Cons.Count; $i++) {
        $Terminal += [PSCustomObject]@{
            Name = $Cons[$i]
            Guid = $DCs[$i], $DTs[$i]
            Item = $Names
            Path = $regP
        }
    }

    if ($default -or $d) {
        if ($Guids) {
            return $Terminal | Where-Object { $_.Guid[0] -eq $Guids[0] }
        }
    } else {
        return $Terminal
    }
}

function Set-Terminal 
{    
    <#
    .SYNOPSIS
    Sets the default terminal.
 
    .DESCRIPTION
    The Set-Terminal function allows the user to set the default terminal by selecting from available options or
    by providing a predefined option via parameters. It modifies the registry settings to update the default terminal.
 
    .EXAMPLE
    Set-Terminal
    Prompts the user to select the default terminal from a list.
 
    .EXAMPLE
    Set-Terminal 'wt'
    Sets the default terminal to Windows Terminal.
 
    .NOTES
    Author: Your Name
    #>

    
    $Terminals = Get-Terminal
    $DefaulT   = Get-Terminal -default
    $regP      = "HKCU:\Console\%%Startup"
    $Names     = (Get-Item -Path $regP).Property
    $TNames    = $Terminals.Name
    
    if ($Args) {
        Switch ("$Args") {
            {$_ -in $TNames[0], 'lwd'} {$def = 0}
            {$_ -in $TNames[1], 'wch'} {$def = 1}
            {$_ -in $TNames[2], 'wt' } {$def = 2}
        }
        
        if (-not $def) {Return}
        if ($Terminals[$def].Name -eq $DefaulT.Name) {Return}
        
    } else {
        Clear-Host
        For ($i = 0; $i -lt $Terminals.Count; $i++) {
            $n = $i + 1
            if ($Terminals[$i].Name -eq $DefaulT.Name) {
                Write-Host ("[{0}] {1}" -f $n, $Terminals[$i].Name) -f green
                $Exc = $n
            } else {
                Write-Host ("[{0}] {1}" -f $n, $Terminals[$i].Name)
            }
        }
        
        choice.exe /c "123x" /m '(X) Return:'
        if ($LastExitCode -eq 4) {Clear-Host; Return}
        if ($LastExitCode -eq $Exc) {Set-Terminal}
        $def = $LastExitCode - 1
    }
    
    $ToDef = $Terminals[$def]
    Set-ItemProperty -Path $regP -Name "$($Names[0])" -value $ToDef.Guid[0]
    Set-ItemProperty -Path $regP -Name "$($Names[1])" -value $ToDef.Guid[1]
    
    if (!$Args) {Set-Terminal}
}