question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PSCommandNotFoundSuggestion is suggesting commands when it should not

See original GitHub issue

Steps to reproduce

If you use Get-Command -ErrorAction Ignore to test for the existence of a command, you get “Suggestion” output which is confusing to the end user and seems to indicate an error, when in fact there is no problem.

For example, in one script I do this:

$Version = if (Get-Command gitversion -ErrorAction SilentlyContinue) { 
    gitversion -showvariable semver
} else { 
   "1.0.0-rc"
}

Expected behavior

When Get-Command is called and the error is explicitly suppressed, there should be no suggestions printed to the console.

Frankly, it might be better if Get-Command doesn’t print suggestions at all, or perhaps only when it’s being called interactively from the prompt (and the error isn’t explicitly suppressed).


Actual behavior

I get a completely off-the-wall suggestion from the PSDeploy module:

Suggestion [4,General]: The most similar commands are: WithOptions

Frankly, that command is not at all similar, in my human brain, and it really should not be mentioned at all. I can’t understand how that’s past the similarity threshold.

Environment data

Name                           Value
----                           -----
PSVersion                      6.2.1
PSEdition                      Core
GitCommitId                    6.2.1
OS                             Microsoft Windows 10.0.18362
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
vexx32commented, Aug 27, 2019

Yeah we need to redo how suggestions are implemented as a whole, really. Currently suggestions are entirely dependent on and coded within PowerShell’s host implementation. Which means they don’t carry over to anyone using a non-default host.

I have a PR #9115 WIP that addresses this, pulls it all out and puts suggestions into a property of the ErrorRecord class with a new data type, which can then be displayed by formatters instead and is accessible to module authors. Got stuck on a few bits and pieces and CI errors that I couldn’t make sense of. I’ll revisit once I’m done working on Test-Connection and see if I can get it properly up to scratch. 🙂

Or, if one of you folks wants to take a stab at it, you’re more than welcome to check it out, add comments, and/or just grab what you need (if anything) and take over as I may or may not have enough spare time to get it done by PS7 (and I’d really like this to be squared away by PS7, but then there are a lot of things I’d like to get done for PS7 hah!)

1reaction
bergmeistercommented, Jul 25, 2019

Yes, I agree that this definitely needs tweaking. But I’ve always also found it annoying that Get-Command returns an error if nothing was found (yes, I had to write similar code to yours before as well…). It would be a breaking change but in an ideal world the command should just return nothing and the caller can then write conditional logic. What annoys me about Get-Command as well is that it loads the module into memory by doing a full Import-Module (which makes this check very expensive). For a binary module this means I am stuck with that loaded version since PS cannot unload it (maybe that is possible with recent additions in .Net Core?).

In order to avoid breaking changes, what about a more lightweight and user friendly Test-Command cmdlet?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Experimental Features in PowerShell
PSCommandNotFoundSuggestion. Recommends potential commands based on fuzzy matching search after a CommandNotFoundException.
Read more >
PowerShell 7 Experimental Features - Dave Carroll
With the PSCommandNotFoundSuggestion experimental feature enabled, PowerShell can suggest commands when we have a typo or just space out. Thanks ...
Read more >
PowerShell Core and Experimental Features
The PSCommandNotFoundSuggestion just helps IT Admins to find what they need. And the batching of implicit remoting commands could be very useful ...
Read more >
Suggest command not working in discord.py
I'm trying to make a command that you can suggest something to the server like they make a channel called suggestion-channel, when they...
Read more >
Powershell Core – “experimental feature” feature - AndySvints
An experimental feature is one where the design is not finalized. ... PSCommandNotFoundSuggestion – If/when you will mistype cmdlet it will ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found