I would like to be able to define and use a custom type in some of my PowerShell scripts. For example, let's pretend I had a need for an object that had the following structure:
Contact { string First string Last string Phone }
How would I go about creating this so that I could use it in function like the following:
function PrintContact { param( [Contact]$contact ) "Customer Name is " + $contact.First + " " + $contact.Last "Customer Phone is " + $contact.Phone }
Is something like this possible, or even recommended in PowerShell?
With the help of the Select-Object cmdlet, you can add a calculated property to the output of a PowerShell command. This allows you to enrich the displayed information.
Long description. The [pscustomobject] type accelerator was added in PowerShell 4.0. Prior to adding this type accelerator, creating an object with member properties and values was more complicated. Originally, you had to use New-Object to create the object and Add-Member to add properties.
The Add-Member cmdlet lets you add members (properties and methods) to an instance of a PowerShell object. For instance, you can add a NoteProperty member that contains a description of the object or a ScriptMethod member that runs a script to change the object.
PowerShell's Extensible Type System didn't originally let you create concrete types you can test against the way you did in your parameter. If you don't need that test, you're fine with any of the other methods mentioned above.
If you want an actual type that you can cast to or type-check with, as in your example script ... it cannot be done without writing it in C# or VB.net and compiling. In PowerShell 2, you can use the "Add-Type" command to do it quite simmple:
add-type @" public struct contact { public string First; public string Last; public string Phone; } "@
Historical Note: In PowerShell 1 it was even harder. You had to manually use CodeDom, there is a very old function new-struct script on PoshCode.org which will help. Your example becomes:
New-Struct Contact @{ First=[string]; Last=[string]; Phone=[string]; }
Using Add-Type
or New-Struct
will let you actually test the class in your param([Contact]$contact)
and make new ones using $contact = new-object Contact
and so on...
If you don't need a "real" class that you can cast to, you don't have to use the Add-Member way that Steven and others have demonstrated above.
Since PowerShell 2 you could use the -Property parameter for New-Object:
$Contact = New-Object PSObject -Property @{ First=""; Last=""; Phone="" }
And in PowerShell 3, we got the ability to use the PSCustomObject
accelerator to add a TypeName:
[PSCustomObject]@{ PSTypeName = "Contact" First = $First Last = $Last Phone = $Phone }
You're still only getting a single object, so you should make a New-Contact
function to make sure that every object comes out the same, but you can now easily verify a parameter "is" one of those type by decorating a parameter with the PSTypeName
attribute:
function PrintContact { param( [PSTypeName("Contact")]$contact ) "Customer Name is " + $contact.First + " " + $contact.Last "Customer Phone is " + $contact.Phone }
In PowerShell 5 everything changes, and we finally got class
and enum
as language keywords for defining types (there's no struct
but that's ok):
class Contact { # Optionally, add attributes to prevent invalid values [ValidateNotNullOrEmpty()][string]$First [ValidateNotNullOrEmpty()][string]$Last [ValidateNotNullOrEmpty()][string]$Phone # optionally, have a constructor to # force properties to be set: Contact($First, $Last, $Phone) { $this.First = $First $this.Last = $Last $this.Phone = $Phone } }
We also got a new way to create objects without using New-Object
: [Contact]::new()
-- in fact, if you kept your class simple and don't define a constructor, you can create objects by casting a hashtable (although without a constructor, there would be no way to enforce that all properties must be set):
class Contact { # Optionally, add attributes to prevent invalid values [ValidateNotNullOrEmpty()][string]$First [ValidateNotNullOrEmpty()][string]$Last [ValidateNotNullOrEmpty()][string]$Phone } $C = [Contact]@{ First = "Joel" Last = "Bennett" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With