How can we programmatically alter the PowerShell console settings in Windows 10?
When I install a fresh copy of Windows, I would like the PowerShell console adjusted as follows:
- Font size 18
- Screen Buffer Size 9999
- Window Size 140
- Window Height 32
It sounds like this should be very simple to do, but I have not found a way yet.
Initially, this seemed like a pathway to doing it but cannot get it to work:
$fontSize = 18
$screenBufferSize = 9999
$windowSize = 140
$windowHeight = 32
function Set-ConsoleSettings {
param (
[int]$FontSize,
[int]$ScreenBufferSize,
[int]$WindowSize,
[int]$WindowHeight
)
$font = New-Object System.Drawing.Font("Consolas", $FontSize)
$host.UI.RawUI.GetType().GetProperty("FontSize").SetValue($host.UI.RawUI, $font.Size, $null)
$host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size($ScreenBufferSize, $WindowSize)
$host.UI.RawUI.WindowSize = New-Object Management.Automation.Host.Size($WindowSize, $WindowHeight)
}
# Change regular (non-admin) PowerShell console
Set-ConsoleSettings -FontSize $fontSize -ScreenBufferSize $screenBufferSize -WindowSize $windowSize -WindowHeight $windowHeight
# Change elevated (admin) PowerShell console
Start-Process powershell.exe -ArgumentList "-NoProfile -Command Set-ConsoleSettings -FontSize $fontSize -ScreenBufferSize $screenBufferSize -WindowSize $windowSize -WindowHeight $windowHeight" -Verb RunAs
Alternatively, maybe creating a custom shortcut with the settings defined in it, and then pinning that to the Taskbar could be a solution:
# Define the desired settings
$fontSize = 18
$screenBufferSize = 9999
$windowSize = 140
$windowHeight = 32
# Create a PowerShell shortcut with the desired font size
$shortcutPath = "$env:USERPROFILE\Desktop\CustomPowerShell.lnk"
$shell = New-Object -ComObject WScript.Shell
$shortcut = $shell.CreateShortcut($shortcutPath)
$shortcut.TargetPath = "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe"
$shortcut.Arguments = "-NoProfile -ExecutionPolicy Bypass -Command `$host.UI.RawUI.FontSize=$fontSize; `$host.UI.RawUI.BufferSize=New-Object Management.Automation.Host.Size($screenBufferSize, $windowSize); `$host.UI.RawUI.WindowSize=New-Object Management.Automation.Host.Size($windowSize, $windowHeight)"
$shortcut.Save()
Write-Host "Custom PowerShell shortcut created on desktop."
Again, this fails.
I'm not really looking for "install Windows Terminal" as an answer as, although I love Windows Terminal, this is about configuring out of the box Windows so these settings should be something that I can do within an autounattend.xml to distribute standardised settings (and in any case, Windows Terminal is installed by default in Windows 11 I think).
According to Editing shortcut (.lnk) properties with Powershell there seems to be no real way to set relevant properties of a shortcut file using PowerShell.
You can however create link file and pin it to the taskbar.
I suppose the code could be modified to copy a lnk file instead of creating one.