Push-Location as a function's parameter

I’m looking for a way to use push-location with a function’s parameter. My goal is that a user will type "Import-SomeFile -Path ". When the user hits tab, then -Path would push-location to a network share to recurse through that directory. Is this possible/feasible/advisable?

So, you want to enable parameter auto-complete for some pre-determined network location?

Yes sir, that’s the goal.

Start here - https://foxdeploy.com/2017/01/13/adding-tab-completion-to-your-powershell-functions/. Stephen did an excellent job laying out the basic framework. You need a Dynamic Parameter.

Fantastic, thanks for the blazing fast responses. I’ll take a look!

Hi Colin,

In addition to the above, functionality for dynamic ValidateSet is coming in the next PowerShell release, keep an eye out!
https://github.com/PowerShell/PowerShell/pull/3784

Liam

Liam, thanks for the heads up! This is my first experience with Dynamic Parameters and they do seem to have a lot of code around them for this functionality to work, so it’ll be exciting to see that get folded in and make it more succinct. I’ll keep an eye out.

Here’s an example of the unreleased PowerShell 6 functionality (as per your requirements 'caus I’m nice)

class ValidateSetPath : System.Management.Automation.IValidateSetValuesGenerator
{
    [string[]] GetValidValues()
    {
        return (Get-ChildItem -Path '\\Network\Path' -File).Name
    }
}

function Test-ValidateSet
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateSet([ValidateSetPath])]
        [string[]]
        $Item
    )

    $Item | ForEach-Object {
        $Path = Join-Path -Path '\\Network\Path' -ChildPath $_

        Get-Item -Path $Path
    }
}

That’s super helpful. I’m going to get that incorporated right in! :slight_smile:

Not too soon, it’s still in beta :stuck_out_tongue: