0

我只想在从Windows Terminal启动 PowerShell 时导入一些模块(posh-git、oh-my-posh 等) 。当使用 conhost 或从 Cmder 启动 PowerShell 时,应排除这些导入。

但是当我从 Windows 终端或 conhost 或 Cmder 打开 PowerShell 时,我可以看到它$profile指向同一个文件。

有没有办法可以识别当前正在使用的终端,以便我可以在我的$profile文件中执行类似的操作?

If ($TERM -eq 'WT') {
    Import-Module posh-git
    Import-Module oh-my-posh
} 
4

1 回答 1

1

您可以使用自动变量之一WSLENVWT_PROFILE_IDWT_SESSION检查 Powershell 是否在 Windows 终端中运行。它们不存在于“独立”Powershell 中。

例如:

function Test-IsWindowsTerminal { [bool]($env:WT_Session)}
于 2020-07-25T11:18:02.847 回答