1
votes

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
4
start powershell '-NoExit -command "$Host.UI.RawUI.WindowTitle = ''bits''"'user4003407

4 Answers

4
votes

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 ''.
1
votes

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'"
0
votes

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'
0
votes

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....