Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

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

share|improve this answer
    
Wow, your second solution is truly evil :)) Unfortunately it doesn't work for programs not in PATH, like scripts in the current folder –  Adal Oct 1 '10 at 22:48

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

share|improve this answer
    
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" –  Adal Jul 30 '10 at 19:13

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.

share|improve this answer
    
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. –  Adal Aug 10 '10 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 '13 at 16:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.