gpt4 book ai didi

powershell - 在 PowerShell session 之间保留当前目录

转载 作者:行者123 更新时间:2023-12-03 00:37:12 25 4
gpt4 key购买 nike

有没有办法让 PowerShell 控制台记住上次运行时的当前目录并将其用作下一个实例的默认位置?

例如。

  • 打开 PowerShell 控制台
  • 设置位置 c:\tmp
  • 关闭 PowerShell
  • 打开 PowerShell 控制台
  • Get-Location 返回 c:\tmp

最佳答案

您可以更改 prompt 函数以将位置更改保存到用户环境变量:

$Function:prompt = {
if([System.Environment]::GetEnvironmentVariable("LocationMemory",[System.EnvironmentVariableTarget]::User) -ne $PWD){
[System.Environment]::SetEnvironmentVariable("LocationMemory",$PWD,[System.EnvironmentVariableTarget]::User)
}
"PS $($ExecutionContext.SessionState.Path.CurrentLocation)$('>' * ($NestedPromptLevel + 1))"
}

然后在powershell启动时检查环境变量是否存在且有效

if(($LastLocation = [System.Environment]::GetEnvironmentVariable("LocationMemory",[System.EnvironmentVariableTarget]::User))){
if((Test-Path $LastLocation -PathType Container)){
Set-Location $LastLocation
}
}

将这两个片段放入您的个人资料中以使其发挥作用。您可能希望将其限制为 $Profile.CurrentUserCurrentHost

关于powershell - 在 PowerShell session 之间保留当前目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32261508/

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