Tip: Use a Type Constraint in Windows PowerShell

Follow Our Daily Tips

Twitter | Blog | RSS | Facebook

When accepting parameters for a function, it may be important to use a type constraint to ensure that the function receives the correct type of data. To do this, you can place the desired data type alias inside square brackets in front of the input parameter. This action constrains the data type and prevents the entry of an incorrect type of data.

Allowable type shortcuts are shown here:
[int] 32-bit signed integer
[long] 64-bit signed integer
[string] Fixed-length string of Unicode characters
[char] Unicode 16-bit character
[bool] True/false value
[byte] 8-bit unsigned integer
[double] Double-precision 64-bit floating point number
[decimal] 128-bit decimal value
[single] Single-precision 32-bit floating point number
[array] Array of values
[xml] Xmldocument object
[hashtable] Hashtable object (similar to a Dictionary object)

From the Microsoft Press book Windows PowerShell 2.0 Best Practices by Ed Wilson with the Windows PowerShell Teams at Microsoft.

Looking for More Tips?

For more tips on using Microsoft products and technologies, visit the TechNet Magazine Tips library.