【分享】Windows Terminal + PowerShell 7.x 如何保留上次的 tab 和目录

2026-04-29 09:500阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐
问题描述:

Windows Terminal 设置保留上次的 tab,settings.json 根级加:

{ "firstWindowPreference": "persistedWindowLayout" }

或者设置

image993×626 31.6 KB

查看 PowerShell 的配置文件位置,在 PoweShell 里执行:

echo $PROFILE

输出:

C:\Users\xxxx\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

把这段放进 $PROFILE,让终端记住当前目录:

# 首次使用时创建 PowerShell 配置文件 if (-not (Test-Path $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force | Out-Null } # 仅在 Windows Terminal 启动时清屏 if ($env:WT_SESSION) { Clear-Host } # 保存当前已有的 prompt,后面继续沿用原来的提示符样式 if (-not $Global:__OriginalPrompt) { $Global:__OriginalPrompt = $function:prompt } function prompt { # 读取当前目录 $loc = $executionContext.SessionState.Path.CurrentLocation # 通过 OSC 9;9 把当前目录发给 Windows Terminal # 这样恢复窗口布局时,每个 tab 可以回到上次目录 [char]27 + ']9

阅读全文
问题描述:

Windows Terminal 设置保留上次的 tab,settings.json 根级加:

{ "firstWindowPreference": "persistedWindowLayout" }

或者设置

image993×626 31.6 KB

查看 PowerShell 的配置文件位置,在 PoweShell 里执行:

echo $PROFILE

输出:

C:\Users\xxxx\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

把这段放进 $PROFILE,让终端记住当前目录:

# 首次使用时创建 PowerShell 配置文件 if (-not (Test-Path $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force | Out-Null } # 仅在 Windows Terminal 启动时清屏 if ($env:WT_SESSION) { Clear-Host } # 保存当前已有的 prompt,后面继续沿用原来的提示符样式 if (-not $Global:__OriginalPrompt) { $Global:__OriginalPrompt = $function:prompt } function prompt { # 读取当前目录 $loc = $executionContext.SessionState.Path.CurrentLocation # 通过 OSC 9;9 把当前目录发给 Windows Terminal # 这样恢复窗口布局时,每个 tab 可以回到上次目录 [char]27 + ']9

阅读全文