Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell - How to use 'Start-Process' and rename the newly launched windowtitle

Tags:

powershell

I am trying to use the PowerShell (version 2) Start-Process and rename the newly launched window title. I have the following snippet of code that works fine, it launches a new PowerShell window and tails logs ($c contains the log file to tail):-

Start-Process powershell.exe -Argument "Get-Content", $c, "-wait" 

I am not sure how to include the following so that the newly launched window title can be changed.

$host.ui.RawUI.WindowTitle = 'New window title rename example text'

Cheers.

like image 255
user2977451 Avatar asked Dec 22 '14 17:12

user2977451


2 Answers

A "dirty" solution would be:

start-process powershell.exe -argument "`$host.ui.RawUI.WindowTitle = 'New window title rename example text'; get-content -Path $c -wait"

I would recommend creating a script for you commands and use parameters for input.

Untitled2.ps1

param($c)
$host.ui.RawUI.WindowTitle = 'New window title rename example text'
Get-Content -Path $c -Wait

Script

$script = Get-Item ".\Desktop\Untitled2.ps1"
$c = Get-Item ".\Desktop\t.txt"
Start-Process powershell.exe -ArgumentList "-File $($script.FullName) -c $($c.FullName)"
like image 52
Frode F. Avatar answered Nov 03 '22 19:11

Frode F.


A cleaner powershell solution, especially if you're going to run further commands at the time you spawn the process, might be like this.

$StartInfo = new-object System.Diagnostics.ProcessStartInfo
$StartInfo.FileName = "$pshome\powershell.exe"
$StartInfo.Arguments = "-NoExit -Command `$Host.UI.RawUI.WindowTitle=`'Your Title Here`'"
[System.Diagnostics.Process]::Start($StartInfo)
like image 20
Lou O. Avatar answered Nov 03 '22 17:11

Lou O.