简体   繁体   中英

How to start powershell with a window title?

I have a batch file that allows me to go to particular folder based on my input.

d:
cd d:\test\bits
@ECHO off
cls
:start
ECHO.
ECHO 1. Perl
ECHO 2. Python
set choice=
set /p choice=type in number to go to appropriate code folder:
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto pl
if '%choice%'=='2' goto py
ECHO "%choice%" is not valid, try again
ECHO.
goto start
:pl
cd code\pl
goto end
:py
cd code\py
goto end
:end
start "bits"

At the end of execution, a command prompt window with the title "bits" opens up and is in the specified directory corresponding to the input choice. This is all good. But I want to have the same thing done with Powershell.

If, instead of start "bits" , I put, start powershell , in the last line, I can get Powershell console to open. By doing this, I have two issues.

  1. Powershell console is still in d:\test\bits folder and not in the one I intended it to go.
  2. I cannot get the title to be bits

How do I get the functionality I want with Powershell?

From what I expected and what I was able to reproduce with your script, the current directory is set to the intended one ( d:\\test\\bits\\code\\pl if I enter 1)

For the title part, you can do the following:

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

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

You can start PowerShell with a custom window title using the Start-Process cmdlet and passing the -WindowTitle parameter. Here's an example:

sql

Start-Process powershell.exe -WindowTitle "My Custom Window Title"

This will start a new PowerShell window with the specified window title. You can replace "My Custom Window Title" with any string you like as the window title.

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