在每个命令上更改powershell标题

bir*_*rth 4 powershell

我想在窗口标题中显示我输入到powershell中的最后一个命令,以便于查找

目前我有:

C:\> $host.ui.rawui.WindowTitle = $$
Run Code Online (Sandbox Code Playgroud)

但这只是相对于我输入时的上一个命令,所以如果我有

C:\> cd
C:\> $host.ui.rawui.WindowTitle = $$
Run Code Online (Sandbox Code Playgroud)

标题保持不变,cd而不是随着我给它的每条命令进行更改。有没有一种方法可以设置标题,使其随我输入的每个命令而变化,即

进入

 C:\> cd
Run Code Online (Sandbox Code Playgroud)

将其更改为cd然后

 C:\> python 
Run Code Online (Sandbox Code Playgroud)

将其更改为python

Rom*_*min 6

您可以使用prompt在配置文件中定义的自定义函数。例如:

function prompt {
    # get the last command from history
    $command = Get-History -Count 1

    # set it to the window title
    if ($command) {
        $host.ui.rawui.WindowTitle = $command
    }

    # specify your custom prompt, e.g. the default PowerShell:
    "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
}
Run Code Online (Sandbox Code Playgroud)

注意:在此功能中使用$^$$并没有帮助,它们尚未设置为最后一个命令数据。

  • 这个答案比我的要好。只想添加如何找到您的配置文件:键入$ PROFILE以查看PowerShell配置文件的路径。 (3认同)