1

I am creating a custom PowerShell provider using PowerShellStandard.Library.

When implementing the New-Item cmdlet, I would like to be able to provide support for argument completion of the -ItemType parameter.

I cannot find any documentation from Microsoft for how they did it for the built-in providers. If you are in a path of the FileSystem, you get tab-completion for values like "File", "Directory".

Is there any way that I can specify possible values for the ItemType/Type parameter?

I can't using the [ArgumentCompleter] attribute normally since the parameter is not defined in my code. (You can't use that attribute on the itemTypeName parameter in C#)

I have tried to override the parameter with the NewItemDynamicParameters method:

protected override object NewItemDynamicParameters(string path, string itemTypeName, object newItemValue)
{
    return new NewItemDynamicParameters();
}

internal sealed class NewItemDynamicParameters
{
    [Parameter, ArgumentCompleter(typeof(ItemTypeCompleter))]
    public string ItemType { get; set; }
}

internal class ItemTypeCompleter : IArgumentCompleter
{
    public IEnumerable<CompletionResult> CompleteArgument(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
    {
        return new[] { "Value1", "Value2" }.Select(s => new CompletionResult(s));
    }
}

but that just results in this error: (It also didn't try to complete the argument)

A parameter with the name 'ItemType' was defined multiple times for the command.
At line:1 char:1
+ New-Item 1 -ItemType test
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], MetadataException
    + FullyQualifiedErrorId : ParameterNameAlreadyExistsForCommand

0 Answers0