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.

When creating a scriptblock for Register-ArgumentCompleter Faster Result using Object imbedded Where and ForEach

See original GitHub issue

Summary of the new feature/enhancement

$wordToComplete* filter should always be used at the end of the scriptblock, otherwise you will give unexpected result to the user.

Another suggetion is to use object embeded Where and ForEach:

(
<your code>
).Where( {
    $_.Name -like "$wordToComplete*"
 }).ForEach( {
    [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name)
 })

This will work faster than:

(
<your code>
) | Where-Object {
     $_.Name -like "$wordToComplete*"
} | ForEach-Object {
     [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name)
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
mklement0commented, Jan 25, 2021

@B-Art, there are two options, which aren’t mutually exclusive:

  • If you feel the documentation is lacking / needs correction / a new topic should be created, open issues (and possible create PRs) at the documentation repo, https://github.com/MicrosoftDocs/PowerShell-Docs/issues (you can also create an issue direct from an existing online help topic to suggest a correction or improvement to it, specifically.

  • If you (possibly in addition) want to share PowerShell tips, use a forum such as Stack Overflow (you can ask a question that describes your scenario and then self-answer).

    • That said, you may find that such tips already exist; for instance, this answer discusses the relative performance of the ForEach-Object cmdlet vs. the .ForEach() array method, and this answer shows use of the parameter-level [ArgumentCompleter()] attribute.

Other forums:

  • The Slack and Discord community chat forums (they seamlessly talk to each other).

  • The PowerShell.org forum for searching or posting questions about PowerShell.

<div> GitHub</div><div>MicrosoftDocs/PowerShell-Docs</div><div>The official PowerShell documentation sources. Contribute to MicrosoftDocs/PowerShell-Docs development by creating an account on GitHub.</div>
1reaction
B4Artcommented, Jan 26, 2021

Thanx again @mklement0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Register-ArgumentCompleter (Microsoft.PowerShell.Core)
Within the script block, the first command retrieves all running services using the Where-Object cmdlet. The services are piped to the ForEach-Object cmdlet....
Read more >
PowerShell | Argument completer not working when a file ...
cer extension when I press TAB button. Here is one of them $ArgumentCompleterCertPath = { Get-ChildItem | where-object { $_.extension - ...
Read more >
Powershell | James O'Neill's Blog | Page 2 - WordPress.com
Moving the script block out of the call to Register-ArgumentCompleter and into a parameter attribute is simple enough, but it takes a bit ......
Read more >
Improvements in ForEach and ForEach-Object
In PowerShell 7, both of these iteration methods are considerably faster. Using either ForEach ... Defining a long-running script block using ForEach-Object.
Read more >
Is it possible to validate/autocomplete an input parameter ...
I'm trying to see if it's possible to write a powershell function that uses windows services as a validateset to tab autocomplete the...
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