external help file | Module Name | online version | schema |
---|---|---|---|
PSTypeExtensionTools-help.xml |
PSTypeExtensionTools |
2.0.0 |
Add a new type extension.
Add-PSTypeExtension [-TypeName] <String> -MemberType <String> -MemberName <String> -Value <Object> [-IncludeDeserialized] [-WhatIf] [-Confirm] [<CommonParameters>]
Use this command to define a new type extension for a given object type. Existing members with the same name will be overwritten, so you can also use this as a "Set" command.
PS C:\> Add-PSTypeExtension -TypeName system.string -MemberType AliasProperty -MemberName Size -Value Length
Define an alias property called Size for the Length property of the String type.
PS C:\> Add-PSTypeExtension -TypeName system.string -MemberType ScriptMethod -MemberName IsIPAddress -value {$this -match "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"}
PS C:\> $s = "10.4.7.10"
PS C:\> $s.IsIPAddress()
True
Define a script property called IsIPAddress which will return True if the string matches the pattern for an IP address.
PS C:\ Add-PSTypeExtension -TypeName System.IO.FileInfo -MemberType AliasProperty -MemberName Size -value Length -IncludeDeserialized
Create an alias called Size using the Length property on file objects. This expression will also create it for the deserialized version of the type so that you can use it with remoting results.
PS C:\> Show-Command Add-PSTypeExtension
Here is an easy way to get a GUI to create a new type extension.
The name of your type extension.
Type: String
Parameter Sets: (All)
Aliases: Name
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
The member type. You cannot use this command to define CodeMethods or CodeProperties.
Type: String
Parameter Sets: (All)
Aliases: Type
Accepted values: AliasProperty, NoteProperty, ScriptProperty, ScriptMethod
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Enter the name of a type like System.IO.FileInfo.
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: 0
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False
The value for your type extension. Remember to enclose scriptblocks in {} and use $this.
Type: Object
Parameter Sets: (All)
Aliases:
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Prompts you for confirmation before running the cmdlet.
Type: SwitchParameter
Parameter Sets: (All)
Aliases: cf
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Shows what would happen if the cmdlet runs. The cmdlet is not run.
Type: SwitchParameter
Parameter Sets: (All)
Aliases: wi
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Create the extension in the deserialized version of the specified type along with the specified type.
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.
Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/