From 1d21e707b99500629a4d4b07b7c12cff485d3615 Mon Sep 17 00:00:00 2001 From: GraceSolutions Date: Tue, 5 May 2026 17:20:31 -0400 Subject: [PATCH] Configure default SSH shell with precedence: PS7 > PS5 > unchanged - Checks for PowerShell 7 at Program Files\PowerShell\7\pwsh.exe - Falls back to PowerShell 5 at System32\WindowsPowerShell\v1.0\powershell.exe - Leaves default shell unchanged if neither found - Sets HKLM\SOFTWARE\OpenSSH\DefaultShell registry value - Sets DefaultShellCommandOption to '-NoLogo -NoProfile -Command' - Idempotent: only updates registry if value differs --- Invoke-OpenSSHConfiguration.ps1 | 75 +++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/Invoke-OpenSSHConfiguration.ps1 b/Invoke-OpenSSHConfiguration.ps1 index 814545f..d26de00 100644 --- a/Invoke-OpenSSHConfiguration.ps1 +++ b/Invoke-OpenSSHConfiguration.ps1 @@ -928,6 +928,81 @@ Switch (Test-ProcessElevationStatus) } #endregion + #region Configure Default Shell (PowerShell 7 > PowerShell 5 > No Change) + $OpenSSHRegistryPath = 'HKLM:\SOFTWARE\OpenSSH' + + #Check for PowerShell 7 first, then PowerShell 5 + $PowerShell7Path = [System.IO.FileInfo]"$($Env:ProgramFiles)\PowerShell\7\pwsh.exe" + $PowerShell5Path = [System.IO.FileInfo]"$($Env:SystemRoot)\System32\WindowsPowerShell\v1.0\powershell.exe" + + $DefaultShellPath = $Null + $DefaultShellCommandOption = $Null + + Switch ($True) + { + {($PowerShell7Path.Exists -eq $True)} + { + $DefaultShellPath = $PowerShell7Path.FullName + $DefaultShellCommandOption = '-NoLogo -NoProfile -Command' + $WriteLogMessage.Invoke(0, @("PowerShell 7 detected at `"$($DefaultShellPath)`".")) + } + + {($PowerShell5Path.Exists -eq $True)} + { + $DefaultShellPath = $PowerShell5Path.FullName + $DefaultShellCommandOption = '-NoLogo -NoProfile -Command' + $WriteLogMessage.Invoke(0, @("PowerShell 5 detected at `"$($DefaultShellPath)`".")) + } + + Default + { + $WriteLogMessage.Invoke(1, @("No PowerShell installation detected. Leaving default shell unchanged.")) + } + } + + Switch ($Null -ine $DefaultShellPath) + { + {($_ -eq $True)} + { + #Ensure OpenSSH registry key exists + Switch (Test-Path -Path $OpenSSHRegistryPath -ErrorAction SilentlyContinue) + { + {($_ -eq $False)} + { + $WriteLogMessage.Invoke(0, @("Creating OpenSSH registry key. Please Wait...")) + $Null = New-Item -Path $OpenSSHRegistryPath -Force -ErrorAction SilentlyContinue + } + } + + #Get current values + $CurrentDefaultShell = Get-ItemProperty -Path $OpenSSHRegistryPath -Name 'DefaultShell' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'DefaultShell' -ErrorAction SilentlyContinue + $CurrentDefaultShellCommandOption = Get-ItemProperty -Path $OpenSSHRegistryPath -Name 'DefaultShellCommandOption' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'DefaultShellCommandOption' -ErrorAction SilentlyContinue + + #Set DefaultShell if different + Switch ($CurrentDefaultShell -ine $DefaultShellPath) + { + {($_ -eq $True)} + { + $WriteLogMessage.Invoke(0, @("Setting default shell to `"$($DefaultShellPath)`". Please Wait...")) + $Null = Set-ItemProperty -Path $OpenSSHRegistryPath -Name 'DefaultShell' -Value $DefaultShellPath -Type String -Force -ErrorAction SilentlyContinue + $WriteLogMessage.Invoke(0, @("Default shell configured.")) + } + } + + #Set DefaultShellCommandOption if different + Switch ($CurrentDefaultShellCommandOption -ine $DefaultShellCommandOption) + { + {($_ -eq $True)} + { + $WriteLogMessage.Invoke(0, @("Setting default shell command option to `"$($DefaultShellCommandOption)`". Please Wait...")) + $Null = Set-ItemProperty -Path $OpenSSHRegistryPath -Name 'DefaultShellCommandOption' -Value $DefaultShellCommandOption -Type String -Force -ErrorAction SilentlyContinue + $WriteLogMessage.Invoke(0, @("Default shell command option configured.")) + } + } + } + } + #endregion + #region Service Management (Automatic) $SSHDService = Get-Service -Name 'sshd' -ErrorAction SilentlyContinue