Skip to content

Latest commit

 

History

History
195 lines (136 loc) · 4.1 KB

Add-PSTypeExtension.md

File metadata and controls

195 lines (136 loc) · 4.1 KB
external help file Module Name online version schema
PSTypeExtensionTools-help.xml
PSTypeExtensionTools
2.0.0

Add-PSTypeExtension

SYNOPSIS

Add a new type extension.

SYNTAX

Add-PSTypeExtension [-TypeName] <String> -MemberType <String> -MemberName <String> -Value <Object> [-IncludeDeserialized] [-WhatIf] [-Confirm] [<CommonParameters>]

DESCRIPTION

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.

EXAMPLES

EXAMPLE 1

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.

EXAMPLE 2

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.

EXAMPLE 3

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.

EXAMPLE 4

PS C:\> Show-Command Add-PSTypeExtension

Here is an easy way to get a GUI to create a new type extension.

PARAMETERS

-MemberName

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

-MemberType

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

-TypeName

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

-Value

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

-Confirm

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

-WhatIf

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

-IncludeDeserialized

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

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

None

OUTPUTS

None

NOTES

Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/

RELATED LINKS

Get-PSTypeExtension