gpt4 book ai didi

windows - 如何从 RUN 启动具有特定窗口大小的 powershell?

转载 作者:可可西里 更新时间:2023-11-01 13:50:36 28 4
gpt4 key购买 nike

如何从具有特定窗口大小的“RUN”启动 powershell?有没有像“-size:100x100”这样的论点。是否可以使用 RUN 或者是否有任何其他方法来运行给定大小的程序窗口?

最佳答案

从 Windows 运行对话框:

以下命令以默认大小在控制台窗口中启动 PowerShell,然后将窗口大小调整为 100 列 x 50 行:

powershell -noexit -command "[console]::WindowWidth=100; [console]::WindowHeight=50; [console]::BufferWidth=[console]::WindowWidth"

注意:powershell -noexit -command "mode con cols=100 lines=50" 原则上有效,但有一个不幸的副作用,你会丢失任何回滚buffer(缓冲区高度设置为窗口高度)。

该命令使用 [console] ( System.Console ) .NET 类型来设置窗口宽度和高度,另外将缓冲区宽度设置为相同的值作为窗口的宽度,以保证不出现横向滚动条。

从现有的控制台窗口:

如果您从现有的命令提示符或 PowerShell 控制台运行上述命令,新的 PowerShell session 将在当前窗口中启动,并因此调整当前窗口的大小。< br/>以下是打开窗口的方法:

  • 从命令提示符 (cmd.exe):使用 start:

    start powershell -noexit -command "[console]::windowwidth=100; [console]::windowheight=50; [console]::bufferwidth=[console]::windowwidth"
  • 从 PowerShell 控制台窗口:使用 Start-Process(注意参数列表两边的单引号):

    start-process powershell '-noexit -command "[console]::windowwidth=100; [console]::windowheight=50; [console]::bufferwidth=[console]::windowwidth"'

如果您不想在窗口以默认大小打开后调整大小:

上面的命令以默认大小创建新窗口后运行,这可能会破坏视觉效果。

为防止这种情况发生,您有两种选择:

  • 创建一个以 powershell.exe 为目标的快捷方式文件,配置其属性以设置所需的大小,然后运行快捷方式文件 (*.lnk) 打开窗口。

    • 作为papo指出,您还可以通过“开始”菜单、Win-X 快捷菜单或通过文件资源管理器的 File > Open Windows Powershell 为调用的 PowerShell 实例预设窗口大小 命令 这样,通过修改预先存在 底层快捷方式文件:[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.

        • Important: These settings do not apply to console windows started via shortcut files (.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.


Setting 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 进一步指出,“[那个] Win+X 菜单实际上启动了这个 lnk(如果丢失会出错),这很奇怪,因为 Win+X 有自己的快捷方式:” $env:LOCALAPPDATA\Microsoft\Windows\WinX\Group3",将被忽略。(Win10 1809)。如果删除此链接,从资源管理器的 > 文件启动的 PS 仍然有效,但将使用 默认值 来自注册表。"

关于windows - 如何从 RUN 启动具有特定窗口大小的 powershell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43296216/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com