3

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.

Vishwanath
  • 149
  • 2
  • 14

1 Answers1

3

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.

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
  • this gives me the same output whether the process is running or has finished up. Why ? Can we differentiate between tbe two ? – Vishwanath Apr 12 '21 at 09:59
  • Does not look like it is giving the details of process spawned for long running command for 2 reasons. 1) it is giving same reault even after the process has finished. 2) If we close the launching powershell window and try to search for running process in different powershell wondow while the process is still running, it is not able to search it – Vishwanath Apr 12 '21 at 10:10
  • Have your process change the WindowTitle property when it finishes? I had assumed you were expecting the powershell process to not exist when it was done. I'm not sure why you can't find the process from another window. It worked OK for me when I tested. – Mark Wragg Apr 12 '21 at 12:18
  • If you close the launching window, then I suspect you've killed your long running process because you killed its parent process. – Mark Wragg Apr 12 '21 at 12:20