我想将PowerShell窗口的标题更改为当前正在执行的进程的命令行,就像CMD.EXE一样。
我可以在PowerShell中执行此操作吗?
在PowerSHell中执行一个命令时,是否有一些像prompt一样的函数?
你想要一小部分可执行文件吗? 或者所有的exes?
一个可选的数字可执行文件的黑客将做
function cmd { $title = $host.UI.RawUI.WindowTitle $host.UI.RawUI.WindowTitle = 'cmd.exe ' + ($args -join " ") cmd.exe $args $host.UI.RawUI.WindowTitle = $title
}
然后运行cd c:cmd / c dir / s
并看到标题的变化
和所有的命令
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 }
然后尝试ping 127.0.0.1
它的hacky,YMMV
这绝对是可能的,但你必须自己编写代码。 本TechNet文章介绍了如何更改执行窗口的标题行。
是的,这是我的提示function,它将提示中的实际path的最后一部分。 同时设置窗口标题,当你作为pipe理员运行时改变背景,并在标题中添加一个pipe理员:。
$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) { '>>' }) + '> '; }
把它放到你的configuration文件中,记住任何configuration文件都可以直接使用configuration文件variables来访问:$ profile.CurrentUserCurrentHost,$ profile.CurrentuserAllhost ….等。