New advanced functions language features in PowerShell v3

October 24, 2011

no comments

PowerShell 3.0 CTP1 includes many new language enhancements. Here are four of them for advanced functions.

 

CmdletBinding and Parameter attributes

We are no longer required to assign a value of $true to CmdletBinding and Parameter attributes. For example, in v2 we had to write:

 

[CmdletBinding(SupportsShouldProcess=$true)]            
param(            
    [Parameter(Position=0,            
        ValueFromPipeline=$true,            
        ValueFromPipelineByPropertyName=$true            
    )]            
    [string]$Name            
)

In v3 we can omit the assignment.

            
[CmdletBinding(SupportsShouldProcess)]            
param(            
    [Parameter(Position=0,            
        ValueFromPipeline,            
        ValueFromPipelineByPropertyName            
    )]            
    [string]$Name            
)            
            

The absence of an attribute is same as explicitly setting the attribute to $false, but that isn’t new to v3, is the same as v2.

 

Built-in support for Paging operations

[CmdletBinding(SupportsPaging)]

When the SupportsPaging attribute of CmdletBinding is specified in advanced functions we get three additional parameters IncludeTotalCount,Skip, and First. These parameters allows us to page the results. When the IncludeTotalCount is specified the function writes to the host the total amount of objects in the form of:

Total count: n

 

The Skip parameter, UInt64, controls how many objects the function ignores. The value cannot be greater than the total amount of objects in the result. The First parameter, UInt64, controls how many objects the function returns from the beginning of the result.

You can find a script example in the samples folder of CTP1 (Samples\WindowsPowerShell\SupportsPagingSample\SupportsPaging01.ps1).

 

HelpUri

Assign it a web URL and Get-Help will pick it up when the Online switch has been specified, and will

display the page in your default browser. Get-Command also shows the URI in its result.

[CmdletBinding(SupportsPaging,HelpUri='http://PowerShay.com')]             
 

PositionalBinding

[CmdletBinding(PositionalBinding=$false)]             

Setting PositionalBinding to $false allows us to disable positional parameters binding and force users to write the parameter name and its value. See this post for more information.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*