You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Get-Process $pid should suggest using -Id $pid #2722
HacktoberfestPotential candidate to participate in HacktoberfestIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifIssue-Enhancementthe issue is more of a feature request than a bugUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issues
The default parameter set for Get-Process accepts -Name by position.
Maybe it's just me, but I forget this all the time, and expect Get-Process 1234 to work.
I think we can make it work in a reasonable way too. If Name is a positive integer and we do not find a process by that name, search again assuming it was a ProcessId.
Edit: instead of making this work (which might not be a good idea always) - I like the suggestion from Keith Hill (@rkeithhill) to give a better error message.
Reacted by Michael Klement, Rob Holt and Patrick Meinecke
Maybe there should be a defaulted parameterset NameOrID
That won't help if the arg is specified by position but at least it A) gives a way to specify that you really want to match just by name and B) gives a more correct name for the parameter.
Conceivably this could be a breaking change if a script does not use the -Name parameter and there just happens to be a process with a name that corresponds to a PID you want Stop (or otherwise interrogate). That said, it is pretty uncommon to run across processes with a name that parses as a integer. And the workaround is simple - use the -Name parameter. I'd be willing to risk the back compat issue for the convenience provided.
BTW why not give Stop-Process the same treatment? Make a new NameOrId parameter and make it the default parameterset. It annoys me that GPS defaults to name but SPPS defaults to PID. I'm always forgetting to specify the -Name parameter to Stop-Process when I want to stop all instances of an app.
The risk is similar to the discussion the committee had with RFC0011 where files named as integers can produce unexpected results. Perhaps a safe middle ground might be to just tell the user that a process with name 1234 wasn't found and that they should try get-process -pid 1234
Based on the above. There is a possibility to make destructive actions. This is absolutely unacceptable in a script.
And looks very tasty for user input but really requires of informing the user.
I think it makes no sense to do this for a single cmdlet. Let's try to generalize the situation. This is an area of parameter binding. We now offer an alternative parameters before the input of a value, and here we need to offer (informing the user) alternatives after the input of a value (insert a parameter name before the value by tab). Is it possible to do this?
This dovetails nicely into the parameter alias discussion in #2199.
If a user can extend the parameter metadata for other commands, they can add aliases or even an ArgumentTransformationAttribute, which would solve this problem nicely without explicitly opting into the potentially destructive and possibly surprising behavior that I'd like.
Regardless of the outcome here, I'd love to see more PowerShell cmdlets do this:
... tell the user that a process with name 1234 wasn't found and that they should try get-process -pid 1234
I like it when I get that sort of helpful message on Ubuntu e.g. when I run a command that isn't available, Ubuntu tells how to install that command with apt-get.
Also my objection is not about allowing users to add aliases. I just wanted to make sure PowerShell wouldn't add the -rf, -al aliases by default.
PS> get-process 19432
Use pid instead of Name?
The process with name '19432' was not found but 'devenv (pid=19432)' was. Continue?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
4899 643 1841044 1038068 3 789,73 19432 1 devenv
I'm convinced that the default behavior should not be changed, but I'm leaving this open until we have a better error message than Cannot find a process with the name "1234".
Agreed. At the very least, the error message could say Cannont find a process with the name "1234". If 1234 is a process Id, use Get-Process -Id 1234. Of course, you would only use that error message when the Name parameter value is all digits.
If Get-Process and Stop-Process were both changed to have their parameter sets designed like Debug-Process and Wait-Process, where the Name and Id parameters are positional, wouldn't that resolve this problem entirely while making -Process cmdlets consistent in design without introducing breaking changes?
i.e. Today these work:
Get-Process pwsh
Stop-Process $pid
and these don't:
Get-Process $pid
Stop-Process pwsh
If both Get-Process and Stop-Process had positional Id and Name parameters, respectively, wouldn't that make both scenarios work without breaking existing scripts that use positional parameters? I think it would at least resolve the Stop-Process issue without issue, since strings would be automatically mapped to the Name parameter set. I haven't thought all of the paths for this through, and I have my head half paying attention to a debugger right now, but it seems like a possible solution for this.
Kirk Munro (@KirkMunro) As discussed above user can fool himself typing without quotes Stop-Process 1234 and kill process with pid 1234 instead of process with name 1234.
get-process : Cannot find a process with the name "1234". Verify the process name and call the cmdlet again.
At line:1 char:1
+ get-process 1234
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (1234:String) [Get-Process], ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand
It could look like:
get-process : Cannot find a process with the name "1234". Verify the process name and call the cmdlet again.
At line:1 char:1
+ get-process 1234
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (1234:String) [Get-Process], ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand
Suggestion: Did you mean: Get-Process -id 1234 ?
HacktoberfestPotential candidate to participate in HacktoberfestIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifIssue-Enhancementthe issue is more of a feature request than a bugUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issues
Activity
iSazonov commentedon Nov 19, 2016
It's unbelievable but user can actually catching process with name
1234and actually get a process with Pid1234. It's bad.lzybkr commentedon Nov 19, 2016
Right, if finding the process by name fails, only then look by id. Is that not safe and reasonable?
powercode commentedon Nov 19, 2016
I think he means that there is a risk that I have been really clever and created
1234.exe.
It is not running, but when I do
I kill MySuperimportantProcess.exe, with pid 1234.
powercode commentedon Nov 19, 2016
Maybe there should be a defaulted parameterset NameOrID with this behavior, so that you can specify -Name or -ID and only get by name or by id.
rkeithhill commentedon Nov 20, 2016
That won't help if the arg is specified by position but at least it A) gives a way to specify that you really want to match just by name and B) gives a more correct name for the parameter.
Conceivably this could be a breaking change if a script does not use the
-Nameparameter and there just happens to be a process with a name that corresponds to a PID you want Stop (or otherwise interrogate). That said, it is pretty uncommon to run across processes with a name that parses as a integer. And the workaround is simple - use the-Nameparameter. I'd be willing to risk the back compat issue for the convenience provided.BTW why not give
Stop-Processthe same treatment? Make a new NameOrId parameter and make it the default parameterset. It annoys me that GPS defaults to name but SPPS defaults to PID. I'm always forgetting to specify the -Name parameter to Stop-Process when I want to stop all instances of an app.SteveL-MSFT commentedon Nov 20, 2016
The risk is similar to the discussion the committee had with RFC0011 where files named as integers can produce unexpected results. Perhaps a safe middle ground might be to just tell the user that a process with name 1234 wasn't found and that they should try
get-process -pid 1234iSazonov commentedon Nov 20, 2016
Based on the above. There is a possibility to make destructive actions. This is absolutely unacceptable in a script.
And looks very tasty for user input but really requires of
informing the user.I think it makes no sense to do this for a single cmdlet. Let's try to generalize the situation. This is an area of parameter binding. We now offer an alternative parameters before the input of a value, and here we need to offer (
informing the user) alternatives after the input of a value (insert a parameter name before the value by tab). Is it possible to do this?lzybkr commentedon Nov 21, 2016
This dovetails nicely into the parameter alias discussion in #2199.
If a user can extend the parameter metadata for other commands, they can add aliases or even an
ArgumentTransformationAttribute, which would solve this problem nicely without explicitly opting into the potentially destructive and possibly surprising behavior that I'd like.rkeithhill commentedon Nov 21, 2016
Regardless of the outcome here, I'd love to see more PowerShell cmdlets do this:
I like it when I get that sort of helpful message on Ubuntu e.g. when I run a command that isn't available, Ubuntu tells how to install that command with
apt-get.Also my objection is not about allowing users to add aliases. I just wanted to make sure PowerShell wouldn't add the
-rf,-alaliases by default.powercode commentedon Nov 21, 2016
How about something like this?
iSazonov commentedon Nov 22, 2016
Looks good, if it only is in interactive mode but not in script.
2 remaining items
lzybkr commentedon Dec 9, 2016
I'm convinced that the default behavior should not be changed, but I'm leaving this open until we have a better error message than
Cannot find a process with the name "1234".[-]Get-Process $pid should work[/-][+]Get-Process $pid should suggest using `-Id $pid`[/+][-]Get-Process $pid should suggest using `-Id $pid`[/-][+]Get-Process $pid should suggest using -Id $pid[/+]rkeithhill commentedon Dec 9, 2016
Agreed. At the very least, the error message could say
Cannont find a process with the name "1234". If 1234 is a process Id, use Get-Process -Id 1234.Of course, you would only use that error message when the Name parameter value is all digits.lzybkr commentedon Dec 9, 2016
Note that #2199 might as well be the issue to cover customizing parameter metadata.
MohiTheFish commentedon Feb 23, 2019
I am working on this with TylerLeonhardt and Rob Holt (@rjmholt) at HackIllinois 2019. Here is the PR: #8959
KirkMunro commentedon Feb 28, 2019
If
Get-ProcessandStop-Processwere both changed to have their parameter sets designed like Debug-Process and Wait-Process, where the Name and Id parameters are positional, wouldn't that resolve this problem entirely while making -Process cmdlets consistent in design without introducing breaking changes?i.e. Today these work:
and these don't:
If both
Get-ProcessandStop-Processhad positional Id and Name parameters, respectively, wouldn't that make both scenarios work without breaking existing scripts that use positional parameters? I think it would at least resolve theStop-Processissue without issue, since strings would be automatically mapped to the Name parameter set. I haven't thought all of the paths for this through, and I have my head half paying attention to a debugger right now, but it seems like a possible solution for this.iSazonov commentedon Feb 28, 2019
Kirk Munro (@KirkMunro) As discussed above user can fool himself typing without quotes
Stop-Process 1234and kill process with pid 1234 instead of process with name 1234.KirkMunro commentedon Feb 28, 2019
Too bad, makes for annoying parameter sets.
SteveL-MSFT commentedon Mar 6, 2019
We could add something to the suggestion framework when a process named
1234doesn't exist and suggest using the pid if the value is numerical.iSazonov commentedon Mar 6, 2019
I wonder - what can this system return better than the current message?
SteveL-MSFT commentedon Mar 6, 2019
Ilya (@iSazonov) Current error looks like:
It could look like:
iSazonov commentedon Mar 7, 2019
For clarify, now the error meassage is "Cannot find a process with the name "{0}". Try running with -Id to search by Id of processes"