Create default powershell parameter value - current directory

I am hoping to create a parameter for which the default is "current directory" ( .

).

For example, the parameter Path

Get-ChildItem

:

PS> Get-Help Get-ChildItem -Full

      

-Path Specifies the path to one or more locations. Wildcards are allowed. The default 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 parameterized function Path

that takes input from a pipeline with a default value .

:

<#
.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, .

not interpreted as "current directory" in help:

PS> Get-Help Invoke-PipelineTest -Full

      

-Path Specifies the path to one or more locations. Wildcards are allowed. 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 correct way to set the Path

default parameter value for the current directory?

By the way, where is the property set Accept wildcard character

?

+3


source to share


1 answer


Use the attribute PSDefaultValue

to define a custom description for the default value. Use the attribute SupportsWildcards

to mark the 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='.'
    )
}

      

+7


source







All Articles