How to start a powershell from "RUN" with specific window size? is there any argument for that like "-size:100x100". is that possible with RUN or is there any other way to run a program window with a given size?
Open PowerShell using the Run window A fast way to launch this window is to press the Win + R keys on your keyboard. Then, type powershell and press the Enter key or click OK. This method applies to both Windows 11 and Windows 10.
Run Your PowerShell Scripts. After configuring the execution policy, you can run PowerShell scripts. To run a script, open a PowerShell window, type the script's name (with or without the . ps1 extension) followed by the script's parameters (if any), and press Enter.
Run
dialog:The following command starts PowerShell in a console window with the default size and then resizes the window to 100 columns x 50 rows:
powershell -noexit -command "[console]::WindowWidth=100; [console]::WindowHeight=50; [console]::BufferWidth=[console]::WindowWidth"
Note: powershell -noexit -command "mode con cols=100 lines=50"
works in principle, but has the unfortunate side effect that you lose any scroll-back buffer (the buffer height is set to the window height).
The command uses the [console]
(System.Console
) .NET type to set the window width and height, and additionally sets the buffer width to the same value as the window width, so as to ensure that no horizontal scroll bar appears.
If you run the above command from an existing Command Prompt or PowerShell console, the new PowerShell session starts in the current window, and therefore resizes the current window.
Here's how to open a new window:
from a Command Prompt (cmd.exe
): use start
:
start powershell -noexit -command "[console]::windowwidth=100; [console]::windowheight=50; [console]::bufferwidth=[console]::windowwidth"
from a PowerShell console window: use Start-Process
(note the single quotes around the argument list):
start-process powershell '-noexit -command "[console]::windowwidth=100; [console]::windowheight=50; [console]::bufferwidth=[console]::windowwidth"'
The commands above run after the new window has been created with the default size, which can be visually disruptive.
To prevent that, you have two options:
Create a shortcut file that targets powershell.exe
, configure its properties to set the desired size, then run the shortcut file (*.lnk
) to open the window.
File > Open Windows Powershell
command that way, by modifying the preexisting underlying shortcut file:[1]"$env:AppData\Microsoft\Windows\Start Menu\Programs\Windows PowerShell\Windows PowerShell.lnk"
Change the default window size to your liking, but note that this then applies to all PowerShell sessions started with just the executable name (or path):
Interactively:
Press Win+R and submit just powershell
Open the new window's system menu, select Properties
and configure the window size as desired.
Future windows launched the same way will have the same size.
Programmatically:
Console window properties are stored in the registry at HKEY_CURRENT_USER\Console
, with the REG_DWORD
value WindowSize
containing the window size, and ScreenBufferSize
containing the buffer size:
Key HKEY_CURRENT_USER\Console
(HKCU:\Console
) contains the overall defaults.
Subkeys, such as %SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe
, contain overrides for specific executables / window titles.
.lnk
); the latter dynamically inherit the overall console defaults directly from HKEY_CURRENT_USER\Console
(not from any subkeys) - except for CodePage
on Windows 10 (not sure about Windows 8/8.1), and except for values later overridden via the Properties
dialog, which are saved directly in the file.Subkeys inherit values from their parent, which complicates setting values for subkeys - see below for an example.
powershell.exe
window-size defaults programmatically:The following PSv5+ snippet sets the default window size for powershell.exe
-launched console windows to 100 columns by 50 rows.
Note that the fact that screen buffer values are inherited from the overall default settings, stored directly in HKCU:\Console
, adds complexity.
# Determine the target registry key path.
$keyPath = 'HKCU:\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe'
# Get the existing key or create it on demand.
$key = Get-Item $keyPath -ErrorAction SilentlyContinue
if (-not $key) { $key = New-Item $keyPath }
# Determine the new size values.
[uint32] $cols = 100; [uint32] $lines = 50
# Convert to a DWORD for writing to the registry.
[uint32] $dwordWinSize = ($cols + ($lines -shl 16))
# Note: Screen *buffer* values are inherited from
# HKCU:\Console, and if the inherited buffer width is larger
# than the window width, the window width is apparently set to
# the larger size.
# Therefore, we must also set the ScreenBufferSize value, passing through
# its inherited height value while setting its width value to the same
# value as the window width.
[uint32] $dwordScreenBuf = Get-ItemPropertyValue HKCU:\Console ScreenBufferSize -EA SilentlyContinue
if (-not $dwordScreenBuf) { # No buffer size to inherit.
# Height is 3000 lines by default.
# Note that if we didn't set this explicitly, the buffer height would
# default to the same value as the window height.
$dwordScreenBuf = 3000 -shl 16
}
# Set the buffer width (low word) to the same width as the window
# (so that there's no horizontal scrolling).
$dwordScreenBuf = $cols + (($dwordScreenBuf -shr 16) -shl 16)
# Write the new values to the registry.
Set-ItemProperty -Type DWord $key.PSPath WindowSize $dwordWinSize
Set-ItemProperty -Type DWord $key.PSPath ScreenBufferSize $dwordScreenBuf
[1] papo further states, "[That the] Win+X Menu actually starts this lnk (will error if missing), which is weird, as Win+X has its own shortcuts: "$env:LOCALAPPDATA\Microsoft\Windows\WinX\Group3"
, which are ignored. (Win10 1809). PS started from Explorer's > File will still work if this link is removed, but will use defaults from registry."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With