3

I want to change the title of the PowerShell window to the command line of the currently executing process inside it, just like CMD.EXE does.

Can I do this in PowerShell?

Is there some function like prompt which is called when I execute a command in PowerSHell?

3 Answers 3

2

Do you want it for a small select number of executables? Or all exes?

One hack for a select number of executables would to do

function cmd
{
    $title = $host.UI.RawUI.WindowTitle
    $host.UI.RawUI.WindowTitle = 'cmd.exe ' + ($args -join " ")
    cmd.exe $args
    $host.UI.RawUI.WindowTitle = $title
}

Then just run cd c: cmd /c dir /s

And see the title change

And for all the commands

Get-Command -CommandType Application | where {$_.Name -match '.exe$'} | %{
$f = @'
    function {0}
    {{
        $title = $host.UI.RawUI.WindowTitle
        $host.UI.RawUI.WindowTitle = '{0} ' + ($args -join " ")
        {0}.exe $args
        $host.UI.RawUI.WindowTitle = $title
    }}
'@ -f ($_ -replace '.exe', '')
Invoke-Expression $f
}

And then try ping 127.0.0.1

Its hacky, YMMV

1
  • Wow, your second solution is truly evil :)) Unfortunately it doesn't work for programs not in PATH, like scripts in the current folder
    – Meh
    Oct 1, 2010 at 22:48
0

This is definitely possible, though you do have to code for it yourself. This TechNet article describes how to change the Title line of the executing window.

http://technet.microsoft.com/en-us/library/ee156814.aspx

1
  • That article only describes how to statically change the window title. I want a window title which adapts to what runs inside PowerShell. For example, when I type "PS> python.exe <ENTER>", I want the title bar to change to "Administrator: python.exe", just like in CMD.EXE. When python.exe terminates, the title bar should change back to "Administrator: Command Line"
    – Meh
    Jul 30, 2010 at 19:13
0

Yes there is, this is my function for prompt, it put the last part of the actual path in the prompt. Also set the window title and when you runit as admin change the background and add a Admin: in the title.

$FirstRun=1
function prompt{
$shortpath = split-path (Get-Location) -leaf;

$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)

if
($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
   {
   $host.UI.RawUI.WindowTitle = "ADMIN:$shortpath"
   if($FirstRun){$host.UI.RawUI.BackgroundColor = "Black"; cls; $global:FirstRun = 0;}
}
else
{$host.UI.RawUI.WindowTitle = $shortpath}

$(if (test-path variable:/PSDebugContext) 
   { '[DBG]: ' } 
else { '' }) + 'PS ' + $($shortpath) + $(if ($nestedpromptlevel -ge 1) { '>>' }) + '>         ';

}

Put that in your profile, remember that any profile is accesible directly with the profile variable: $profile.CurrentUserCurrentHost, $profile.CurrentuserAllhost ....etc.

2
  • Thanks, but this doesn't do what I want. It only puts the name of the current path in the title bar, not the name of the executable of the current running command.
    – Meh
    Aug 10, 2010 at 7:37
  • One thing you could try is to query for child processes of the current host (via WMI query perhaps?). That should give you the exe being run, which you can put in the title.
    – scobi
    Dec 4, 2013 at 16:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .