< Back

New-DynamicParameter

Sat Jan 11, 2020 1:36 pm

NAME New-DynamicParameter



SYNOPSIS

Expedites creating PowerShell cmdlet dynamic parameters.





SYNTAX

New-DynamicParameter [-Name] <String> [[-Type] <Type>] [-Mandatory] [[-ParameterSets] <String[]>] [[-Position]

<Int32>] [-ValueFromPipeline] [-ValueFromPipelineByPropertyName] [-ValueFromRemainingArguments] [[-HelpMessage]

<String>] [-DontShow] [[-Alias] <String[]>] [-ValidateNotNull] [-ValidateNotNullOrEmpty] [-AllowEmptyString]

[-AllowNull] [-AllowEmptyCollection] [[-ValidateScript] <ScriptBlock>] [[-ValidateSet] <String[]>]

[[-ValidateRange] <Int32[]>] [[-ValidateCount] <Int32[]>] [[-ValidateLength] <Int32[]>] [[-ValidatePattern]

<String>] [[-RuntimeParameterDictionary] <RuntimeDefinedParameterDictionary>] [<CommonParameters>]





DESCRIPTION

This cmdlet facilitates the easy creation of dynamic parameters.





PARAMETERS

-Name <String>

The name of the parameter.



Required? true

Position? 1

Default value

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-Type <Type>

The type of the parameter, this defaults to System.String.



Required? false

Position? 2

Default value string

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-Mandatory [<SwitchParameter>]

Indicates whether the parameter is required when the cmdlet or function is run.



Required? false

Position? named

Default value False

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-ParameterSets <String[]>

The name of the parameter sets to which this parameter belongs. This defaults to __AllParameterSets.



Required? false

Position? 3

Default value @("__AllParameterSets")

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-Position <Int32>

The position of the parameter in the command-line string.



Required? false

Position? 4

Default value -2147483648

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-ValueFromPipeline [<SwitchParameter>]

Indicates whether the parameter can take values from incoming pipeline objects.



Required? false

Position? named

Default value False

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-ValueFromPipelineByPropertyName [<SwitchParameter>]

Indicates that the parameter can take values from a property of the incoming pipeline object that has the same

name as this parameter. For example, if the name of the cmdlet or function parameter is userName, the

parameter can take values from the userName property of incoming objects.



Required? false

Position? named

Default value False

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-ValueFromRemainingArguments [<SwitchParameter>]

Indicates whether the cmdlet parameter accepts all the remaining command-line arguments that are associated

with this parameter.



Required? false

Position? named

Default value False

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-HelpMessage <String>

A short description of the parameter.



Required? false

Position? 5

Default value

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-DontShow [<SwitchParameter>]

Indicates that this parameter should not be shown to the user in this like intellisense. This is primarily to

be used in functions that are implementing the logic for dynamic keywords.



Required? false

Position? named

Default value False

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-Alias <String[]>

Declares a alternative namea for the parameter.



Required? false

Position? 6

Default value @()

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-ValidateNotNull [<SwitchParameter>]

Validates that the argument of an optional parameter is not null.



Required? false

Position? named

Default value False

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-ValidateNotNullOrEmpty [<SwitchParameter>]

Validates that the argument of an optional parameter is not null, an empty string, or an empty collection.



Required? false

Position? named

Default value False

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-AllowEmptyString [<SwitchParameter>]

Allows Empty strings.



Required? false

Position? named

Default value False

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-AllowNull [<SwitchParameter>]

Allows null values.



Required? false

Position? named

Default value False

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-AllowEmptyCollection [<SwitchParameter>]

Allows empty collections.



Required? false

Position? named

Default value False

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-ValidateScript <ScriptBlock>

Defines an attribute that uses a script to validate a parameter of any Windows PowerShell function.



Required? false

Position? 7

Default value

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-ValidateSet <String[]>

Defines an attribute that uses a set of values to validate a cmdlet parameter argument.



Required? false

Position? 8

Default value @()

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-ValidateRange <Int32[]>

Defines an attribute that uses minimum and maximum values to validate a cmdlet parameter argument.



Required? false

Position? 9

Default value

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-ValidateCount <Int32[]>

Defines an attribute that uses maximum and minimum limits to validate the number of arguments that a cmdlet

parameter accepts.



Required? false

Position? 10

Default value

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-ValidateLength <Int32[]>

Defines an attribute that uses minimum and maximum limits to validate the number of characters in a cmdlet

parameter argument.



Required? false

Position? 11

Default value

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-ValidatePattern <String>

Defines an attribute that uses a regular expression to validate the character pattern of a cmdlet parameter

argument.



Required? false

Position? 12

Default value

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



-RuntimeParameterDictionary <RuntimeDefinedParameterDictionary>

The dictionary to add the new parameter to. If one is not provided, a new dictionary is created and returned

to the pipeline.



Required? false

Position? 13

Default value

Accept pipeline input? true (ByPropertyName)

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS

System.Management.Automation.PSObject





OUTPUTS

System.Management.Automation.RuntimeDefinedParameterDictionary





NOTES





AUTHOR: Michael Haken

LAST UPDATE: 2/6/2018



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>DynamicParam {



...



$RuntimeParameterDictionary = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary



New-DynamicParameter -Name "Numbers" -ValidateSet @(1, 2, 3) -Type [System.Int32] -Mandatory

-RuntimeParameterDictionary $RuntimeParameterDictionary | Out-Null



...



return $RuntimeParameterDictionary



}



A new parameter named "Numbers" is added to the cmdlet. The parameter is mandatory and must be 1, 2, or 3. The

dictionary sent in is modified and does not need to be received.









-------------------------- EXAMPLE 2 --------------------------



PS C:\\>DynamicParam {



...



$Params = @(

@{

"Name" = "Numbers";

"ValidateSet" = @(1, 2, 3);

"Type" = [System.Int32]

},

@{

"Name" = "FirstName";

"Type" = [System.String];

"Mandatory" = $true;

"ParameterSets" = @("Names")

}

)



$Params | ForEach-Object {

New-Object PSObject -Property $_

} | New-DynamicParameter

}



The example creates an array of two hashtables. These hashtables are converted into PSObjects so they can match

the parameters by property name, then new dynamic parameters are created. All of the

parameters are fed to New-DynamicParameter which returns a single new RuntimeParameterDictionary to the pipeline,

which is returned from the DynamicParam section.











RELATED LINKS