Validating input
PowerShell provides a variety of different ways to tightly define the content for a parameter. Assigning a .NET type to a parameter is the first of these. If a parameter is set as [string]
, it will only ever hold a value of that type. PowerShell will attempt to coerce any values passed to the parameter into that type.
The PSTypeName attribute
The PSTypeName
attribute can test the type name assigned to a custom object.
It is common in PowerShell to want to pass an object created in one command, as a PSObject
(or PSCustomObject
), to another.
Type names are assigned by setting (or adding) a value to the hidden PSTypeName
property. There are several ways to tag PSCustomObject
with a type name.
The simplest is to set a value for a PSTypeName
property, shown as follows:
$object = [PSCustomObject]@{
Property = 'Value'
PSTypeName = 'SomeTypeName'
}
The PSTypeName
property does not exist on the resulting object, but...