craig
craig

Reputation: 26262

Create powershell parameter default value is current directory

I'm hoping to create a parameter who's default value is the 'current directory' (.).

For example, the Path parameter of Get-ChildItem:

PS> Get-Help Get-ChildItem -Full

-Path Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (.).

    Required?                    false
    Position?                    1
    Default value                Current directory
    Accept pipeline input?       true (ByValue, ByPropertyName)
    Accept wildcard characters?  true

I created a function with a Path parameter that accepts input from the pipeline, with a default value of .:

<#
.SYNOPSIS
Does something with paths supplied via pipeline.
.PARAMETER Path
Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (.).
#>
Function Invoke-PipelineTest {

    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
        [string[]]$Path='.'
    )
    BEGIN {}
    PROCESS {
      $Path | Foreach-Object {
        $Item = Get-Item $_
        Write-Host "Item: $Item"
      }
    }
    END {}
}

However, the . isn't interpreted as the 'current directory' in help:

PS> Get-Help Invoke-PipelineTest -Full

-Path Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (.).

    Required?                    false
    Position?                    1
    Default value                .
    Accept pipeline input?       true (ByValue, ByPropertyName)
    Accept wildcard characters?  false

What's the right way to set the Path parameter's default value to the current directory?

Incidentally, where does one set the Accept wildcard character property?

Upvotes: 4

Views: 2895

Answers (1)

user4003407
user4003407

Reputation: 22102

Use PSDefaultValue attribute to define custom description for default value. Use SupportsWildcards attribute to mark parameter as Accept wildcard characters?.

<#
.SYNOPSIS
Does something with paths supplied via pipeline.
.PARAMETER Path
Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (.).
#>
Function Invoke-PipelineTest {
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
        [PSDefaultValue(Help='Description for default value.')]
        [SupportsWildcards()]
        [string[]]$Path='.'
    )
}

Upvotes: 7

Related Questions