Specifying parameter attributes

In this section, we will discuss the parameter attributes and how to set them. The attributes falling under this category define the different attributes of the parameter itself. Let's take a closer look at the most useful and common options available to define parameter attributes and their uses:

  • Mandatory argument: This argument indicates that this particular parameter is compulsory, otherwise it is optional. For example, if I am writing a function to connect to a vCenter server and doing some work and I want the vCenter name to be provided at runtime, then the following code makes sure that the cmdlet call will fail without the $VCName parameter:
    Param (
      [parameter(Mandatory=$true)]
      [String]$VCName
    )
  • Position argument: We define the positional argument to specify which value will be assigned to which parameter by the position of the values at runtime, without the need to specify the parameter name. PowerShell will understand which parameter the value needs to be assigned to by the order they are provided at runtime. If the position parameter is not specified, then the parameter name must precede the value so that it can be assigned properly. By default, in PowerShell, all the function parameters are positional in nature. At the time of calling the function, if we do not provide the parameter names ahead of the values, then they will be assigned by the position of the parameters. For example, notice the following function, which simply takes the vCenter name and username as input parameters to the function and prints them out. Here, though the $UName parameter is mentioned first, because its position is 1 the second value is assigned to it:
    Function Get-VC{
        [cmdletbinding()]
    
        Param(
        [Parameter(Position=1)]
        [String[]]
        $UName,
    
        [Parameter(Position=0)]
        [String[]]
        $VCName
        )
    
        Write-Host "vCenter Name: $VCName"
        Write-Host "User Name: $UName"
    
    }
    Specifying parameter attributes
  • ValueFromPipeline: This particular argument specifies the parameters the function can take as values from the pipeline. Note that this will enable the function to take the entire object not just a property of the object. To enable this, check and use the ValueFromPipelineByPropertyName argument. Check the following examples. When the ValueFromPipeline argument is not provided, the function does not accept the object value from the pipeline. For example, run the following code:
    Function Get-VC{
        [cmdletbinding()]
    
        Param(
        [Parameter()]
        [String[]]
        $VCName
        )
    
        Write-Host "vCenter Name: $VCName"
    
    }
    Specifying parameter attributes

    When we provide the ValueFromPipeline parameter, it now works as expected:

    Specifying parameter attributes
  • Alias: This attribute provides alternate names for the parameters. It is possible to use as many aliases as needed for the parameters. Check the following example; also note that if we do not mention any parameter name, it still works because, as mentioned earlier, by default PowerShell assigns the value to the parameter that matches the position. For example, let's take a look at the following example where we have mentioned multiple aliases for the $VCName parameter:
    Function Get-VC{
        [cmdletbinding()]
    
        Param(
        [Parameter(Mandatory = $true)]
        [alias('VC','vcenter')]
        [String[]]$VCName
        )
    
        Write-Host "vCenter Name: $VCName"
    
    }

    When we run the function, we get the required output.

    Specifying parameter attributes

    Tip

    The alias name or the parameter is not case sensitive.