Search code examples
c#powershellpowershell-cmdlet

Parameterattribute default value (C# cmdlet) in PowerShell


I'm designing a cmdlet using plain C#. Is it possible to define a default value for a parameter?

Script cmdlet:

[Parameter] [string] $ParameterName = "defaultValue"

Which is the equivalent for C#?

[Parameter]
public string ParameterName { get; set; }

Solution

  • With auto-implemented properties, you can't. You will need to create the actual getter and setter.

    Something like this:

    private string _ParameterName = "defaultvalue";
    
    [Parameter]
    public string ParameterName 
    {
         get
         {
              return _ParameterName ;
         }
         set
         {
             _ParameterName  = value;
         }
    }