Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tab-completion to your PowerShell Functions #35

Open
utterances-bot opened this issue May 6, 2021 · 3 comments
Open

Adding tab-completion to your PowerShell Functions #35

utterances-bot opened this issue May 6, 2021 · 3 comments

Comments

@utterances-bot
Copy link

Adding tab-completion to your PowerShell Functions

FoxDeploy.com, Stephen Owen's technical blog about
PowerShell, Systems Administration, GUI Design and Programming.
.

https://www.foxdeploy.com/blog/adding-tab-completion-to-your-powershell-functions.html

Copy link

Ayanmullick commented May 6, 2021

I'm able to do tab completion for the parameter and the parameter values while running the command from the script pane.

function Hello-World 
    {param( [ValidateSet('Green','Red','Blue')] $Color)
     Write-Host "Hello world!" -Foreground $Color
     }

However, tab completion doesn't work when the file is in the Scripts folder like other scripts installed using Install-Script.

$DestPath = [Environment]::GetFolderPath('MyDocuments')+"\PowerShell\Scripts"
(New-Object System.Net.WebClient).DownloadFile("https://github.com/Ayanmullick/test/raw/master/Hello-World.ps1","$DestPath\Hello-World.ps1")
Invoke-Expression "$DestPath\Hello-World.ps1"

@Ayanmullick
Copy link

Ayanmullick commented May 6, 2021

I'm able to do tab completion for the parameter and the parameter values while running the command from the script pane.

function Hello-World 
    {param( [ValidateSet('Green','Red','Blue')] $Color)
     Write-Host "Hello world!" -Foreground $Color
     }

However, tab completion doesn't work when the file is in the Scripts folder like other scripts installed using Install-Script.

$DestPath = [Environment]::GetFolderPath('MyDocuments')+"\PowerShell\Scripts"
(New-Object System.Net.WebClient).DownloadFile("https://github.com/Ayanmullick/test/raw/master/Hello-World.ps1","$DestPath\Hello-World.ps1")
Invoke-Expression "$DestPath\Hello-World.ps1"

Removing the function wrapper in the file being downloaded and adding the default script folder path to $env:PATH resolved the issue.

If (($env:PATH -split ';') -notcontains $DestPath) {$env:Path += ";$DestPath"}

Copy link

searbr commented May 7, 2023

This was great, thank you on helping me get dynamic tab completion. One part that through me off was that I didn't realize that setting the dynamic param's name is important and must match the name of the variable in the begin.

Set the dynamic parameters' name

$ParameterName = 'Service'

I thought it was just setting the initial value, but if you don't properly set this, the the dynamic portion will fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants