简体   繁体   中英

Set window title when running "start powershell" in powershell not working?

The following command works in CMD ( How to start powershell with a window title? ).

start powershell -NoExit -command "$Host.UI.RawUI.WindowTitle = 'bits'"

But it doesn't work in Powershell.

PS C:\> start powershell -noexit -command "$Host.UI.RawUI.WindowTitle = 'test'; read-host"
Start-Process : A parameter cannot be found that matches parameter name 'noexit'.
At line:1 char:18
+ start powershell -noexit -command "$Host.UI.RawUI.WindowTitle = 'test ...
+                  ~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand

The following command can open a new powershell window.

start powershell "$Host.UI.RawUI.WindowTitle = 'test'; read-host"

However, the new window shows the following error message and the title is not set.

System.Management.Automation.Internal.Host.InternalHost.UI.RawUI.WindowTitle : The term
'System.Management.Automation.Internal.Host.InternalHost.UI.RawUI.WindowTitle' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
At line:1 char:1
+ System.Management.Automation.Internal.Host.InternalHost.UI.RawUI.Wind ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.Manageme...wUI.WindowTitle:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Bacon Bits' helpful answer explains that start in cmd.exe means something different than in PowerShell.

Use Start-Process as follows to get the desired result; note that powershell implicitly binds to parameter -FilePath , whereas the , -separated arguments starting with -NoExit bind implicitly to the -ArgumentList ( -Args ) parameter, which accepts an array of strings :

# In PowerShell, `start` is an alias for `Start-Process`
start powershell '-NoExit', '-command', "`$Host.UI.RawUI.WindowTitle = 'bits'"

In paticular, - -prefixed pass-through arguments must be quoted so that they're not mistaken for Start-Process 's own parameters.

Also note the ` preceding the $ in $Host , which prevents up-front interpolation of $Host by the calling PowerShell instance.

You could also use '$Host.UI.RawUI.WindowTitle = ''bits''' , a single-quoted literal string with embedded single quotes escaped as '' .


Important :

While passing arguments as an array to -ArgumentList is conceptually the best approach, it is unfortunately ill-advised due to a long-standing bug in Start-Process , still present as of this writing (v7.1) - see GitHub issue #5576 .

For now, using a single string comprising all arguments, enclosed in embedded "..." quoting as necessary, is the only generally robust approach . As discussed in the linked GitHub issue, an -ArgumentArray parameter that supports robust array-based argument passing may be introduced in the future.

In the case at hand this means the following, as suggested by PetSerAl in a comment on the question:

Start-Process powershell '-NoExit -command "$Host.UI.RawUI.WindowTitle = ''bits''"'
  • Note the single -quoting_ ( '...' ) of the overall argument-list string, which then necessitates escaping the embedded single quotes - those that PowerShell should see as part of the command - as '' .

In Command Prompt, start is the start internal command . In Windows Powershell, start is an alias for Start-Process , which does something similar but isn't identical.

Try running this:

powershell -NoExit -command "`$Host.UI.RawUI.WindowTitle = 'bits'"

Here's another way, while avoiding the dollar sign. The double quotes have to be on the outside, so that bits stays quoted.

start powershell "-noexit (get-variable host).value.ui.rawui.windowtitle = 'bits'"

You can always avoid these quoting issues putting the command in a file.

start powershell '-noexit .\window.ps1'

start powershell with the command option for setting the window title did not work for me. Maybe because I want to open another powershell with a ps1 file. so in the other ps1 file I added the first line as below,

(Get-Host).ui.RawUI.WindowTitle='TEST TEST'

and it worked like a charm....

If you add this to your powershell profile.ps1 you can get the window title to show the current running script and if you are just opening a window with no script then 'pwsh' will be displayed.

Will be systematic with no need to add a line on top of each script. The other answers combined with $MyInvocation.MyCommand seem to give the name of the profile.ps1 instead when running a script from the context menu.

This can also be tweaked to change the result.

[console]::title = Split-Path -Leaf ([Environment]::GetCommandLineArgs()[-1]).Replace('pwsh.dll','pwsh')

Works on both PS 5 and 7. For ver. 5 replace pwsh.dll by powershell.exe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM