Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 39101

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:48:43+00:00 2026-05-10T14:48:43+00:00

I would like to be able to define and use a custom type in

  • 0

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?

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:48:44+00:00Added an answer on May 10, 2026 at 2:48 pm

    Prior to PowerShell 3

    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…

    In PowerShell 3

    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

    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' } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 85k
  • Answers 85k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The server is sending the wrong MIME-type. The best suggestion… May 11, 2026 at 5:07 pm
  • Editorial Team
    Editorial Team added an answer The Enterprise Library Logging Application Block (http://msdn.microsoft.com/en-us/library/cc309506.aspx) is built on… May 11, 2026 at 5:07 pm
  • Editorial Team
    Editorial Team added an answer This already exists(returns IEnumerable, but that is easy enough to… May 11, 2026 at 5:07 pm

Related Questions

I would like to know if there is a way to disable automatic loading
I am quite new to ICEfaces but already have experience with JSF/Facelets and the
I very much like the sound of this ElementAtOrDefaultOperator for use with generic lists,
I have a number of solutions with a large number of projects in them.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.