| by Arround The Web | No comments

Learn How PowerShell CmdletBinding Enhances Functions

A function is simply a piece of code that contains instructions that can be used to create output from its input. A function can be reused over and over again. The functionality of a function can be enhanced using the “CmdletBinding” attribute. It helps the function to look and operate like a compiled cmdlet in PowerShell. Doing so will provide the function turned into a cmdlet and access to all the cmdlet features.

The following post will provide details about the attribute “CmdletBinding”.

Learn How PowerShell CmdletBinding Enhances Functions

The attribute “CmdletBinding” is utilized to enhance the function. Particularly, the core function of this attribute is to turn the function into an operable cmdlet.

Examples explaining the stated attribute are given below.

Example 1: Use the “CmdletBinding” Attribute to Transform the String From Upper Case to Lower Case

In this example, the “CmdletBinding” attribute will transform the string to lower case:

Function String-To-LowerCase {
    [CmdletBinding()]Param()
    "THIS IS LINUX HINT PORTAL.".ToLower();
}
String-To-LowerCase

In the mentioned code above:

  • First, create a function and specify a name for it.
  • Then, create a “Param()” and specify the “[CmdletBinding()]” parameter before it.
  • After that, write a string within inverted quotes and concatenate it with the “ToLower()” method.
  • Lastly, call the function by specifying its name outside the curly braces:

Example 2: Use the “CmdletBinding” Attribute in a Function Along With the “-Verbose” Parameter

This demonstration will transform the string into lowercase. Moreover, it will display the verbose message with the aid of the “-Verbose” parameter:

Function String-To-LowerCase {
    [CmdletBinding()]Param()
    Write-Verbose "The -verbose parameter will display the verbose statement."
    "WELC0ME TO THE CONSOLE.".ToLower();
}
String-To-LowerCase -Verbose

In the above-stated code:

  • The verbose statement is given using the “Write-Verbose” cmdlet.
  • Then, the function name is specified outside the curly braces along with the “-Verbose” parameter:

Example 3: Use the “CmdletBinding” Attribute Along With the “SupportsShouldProcess” and “PSCmdlet” Object

This illustration will create a prompt, which will confirm whether to transform the string to upper case or not:

Function String-To-LowerCase {
    [CmdletBinding(SupportsShouldProcess=$True)]Param()
    Write-Verbose "The -verbose parameter will display the verbose statement."
    if ($PSCmdlet.ShouldContinue("Confirm?", "Transform string to LowerCase")) {
        "HELLO WORLD".ToLower();
    } Else {
        "HELLO WORLD"
    }
}

In the above-stated code:

  • First, create a function and specify a name.
  • Inside the function, pass the “SupportsShouldProcess=$True” inside the “CmdletBinding()” attribute.
  • After that, create an “if” condition and pass the “$PSCmdlet.ShouldContinue()” parameter inside it.
  • Then, add the text inside the above-stated parameter to be displayed at the time of getting affirmation from the user.
  • The “if” condition will transform the string to lower-case if the user clicks on the “Yes” button else the string case won’t change:

Click on the “Yes” button to transform the string into a lowercase:

String-To-LowerCase -Confirm

It can be observed that the string has been transformed to lower case.

Conclusion

The “CmdletBinding” attribute in PowerShell is used to convert the function into an operable cmdlet. Doing so will provide access to all cmdlet features to the function turned into a cmdlet. This blog has elaborated on PowerShell’s “CmdletBinding” attribute to enhance the function.

Share Button

Source: linuxhint.com

Leave a Reply