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?

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
Meh
  • 373
  • 2
  • 7
  • 10

3 Answers3

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

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
mrwaim
  • 163
  • 8
  • 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 01 '10 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

sysadmin1138
  • 133,124
  • 18
  • 176
  • 300
  • 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 ", 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 '10 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.

mjsr
  • 181
  • 7
  • 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 '10 at 07: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 04 '13 at 16:54