Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change powershell Title on every command

Tags:

powershell

I would like to display the last command that i have entered into powershell in the title of the window so that it is easier to find

Currently I have:

C:\> $host.ui.rawui.WindowTitle = $$

but this just gets the previous command relative to when i enter it so if I have

C:\> cd
C:\> $host.ui.rawui.WindowTitle = $$

the title stays cd instead of changing with every command i give it. Is there a way I can set the title so it changes with every command I enter, ie

entering

 C:\> cd

will change it to cd and then

 C:\> python 

will change it to python?

like image 779
birthofearth Avatar asked Mar 23 '15 13:03

birthofearth


1 Answers

You can use your custom function prompt defined in your profile. For example:

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)) "
}

Note: use of $^ and $$ in this function does not help, they are not yet set to the last command data.

like image 122
Roman Kuzmin Avatar answered Oct 05 '22 23:10

Roman Kuzmin