I have created a long running process from powershell which runs a command for quite some time as follows:
Start-process "powershell" "long running command"
I want to give this process a custom name or some kind of tag of type string to refer the process later.
Is this possible to name a powershell process launched from powershell ?
I can't use pid in my case because we have to provide our client process-search ability using a name provided by him.
You could have your script modify the WindowTitle property at the start of its execution, as follows:
$host.ui.RawUI.WindowTitle = 'Your Custom Title'
You could then identify your specific process by using Get-Process powershell and checking the MainWindowTitle property.
Get-Process PowerShell | Where-Object { $_.MainWindowTitle -eq 'Your Custom Title' }
There are likely other options also, such as running the process under a specific user account or by targetting a specific PowerShell configuration endpoint and then specifying that when running the PowerShell.exe process via the -ConfigurationEndpoint switch. These require config to be added in advance however, where as modifying the title to something you know will be unique is quite a simple solution.
The MainWindowTitle property may not always be set, such as with the terminal that runs inside VS Code, or sessions started with Start-Job. I found two alternatives, that may be used separately or in combination. In the examples below, each block can be run in a separate window.
CustomPipeName parameterThis parameter is intended for debugging, but we could also just use it as a way to label a process:
First, start a process with a custom label applied:
# Start a test process, and have it tell us about itself so we can verify
$myLabel = 'MyImportantScript'
$argList = @(
'-NoLogo'
'-NoProfile'
'-ExecutionPolicy Bypass'
'-NoExit'
'-Command "& { Get-Process -Id $PID | Format-List -Property Name,Id,Path,CommandLine }"'
"-CustomPipeName '$myLabel'"
)
Start-Process -FilePath 'pwsh.exe' -ArgumentList $argList
Then, find the process by using the label:
# Look for that process by label
$myLabel = 'MyImportantScript'
$p = Get-Process -Name 'pwsh' | Where-Object CommandLine -like "*-CustomPipeName '$myLabel'*"
$p | Format-List Name,Id,Path,CommandLine
This requires some preparation, but may be useful when needing to group and identify larger volumes of processes.
First, create a copy of the PowerShell executable with a new name. This only needs to be done once.
#Requires -RunAsAdministrator
# Get the path of the executable from the current process
$psExecPath = Get-Process -Id $PID | Select-Object -ExpandProperty Path
# Make a copy of the executable with a custom name
$myExecName = 'MyImportantStuff'
$psExecDir = Split-Path -Path $psExecPath -Parent
$myExecPath = Join-Path -Path $psExecDir -ChildPath "$myExecName.exe"
Copy-Item -Path $psExecPath -Destination $myExecPath -PassThru | Select-Object -ExpandProperty FullName
# C:\Program Files\PowerShell\7\MyImportantStuff.exe
Then, run stuff with that new executable:
# Start a test process with that executable, and have it tell us about itself so we can verify
$myExecPath = 'C:\Program Files\PowerShell\7\MyImportantStuff.exe'
$argList = @(
'-NoLogo'
'-NoProfile'
'-ExecutionPolicy Bypass'
'-NoExit'
'-Command "& { Get-Process -Id $PID | Format-List -Property Name,Id,Path }"'
)
Start-Process -FilePath $myExecPath -ArgumentList $argList
And finally, find processes by the executable name:
# Find that process by name
$myExecName = 'MyImportantStuff'
Get-Process -Name $myExecName | Format-List -Property Name,Id,Path
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With