11---
22description : Describes how you can use classes to create your own custom types.
33Locale : en-US
4- ms.date : 01/19/2024
4+ ms.date : 01/22/2026
55online version : https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_classes?view=powershell-5.1&WT.mc_id=ps-gethelp
66schema : 2.0.0
77title : about_Classes
@@ -64,18 +64,17 @@ To instantiate an instance of a class, use one of the following syntaxes:
6464```
6565
6666``` Syntax
67- [$<variable-name> =] [<class-name>]@{[<class-property-hashtable>]}
67+ [$<variable-name> =] [<class-name>]<convertable-value-type>
6868```
6969
7070> [ !NOTE]
7171> When using the ` [<class-name>]::new() ` syntax, brackets around the class name
7272> are mandatory. The brackets signal a type definition for PowerShell.
7373>
74- > The hashtable syntax only works for classes that have a default constructor
75- > that doesn't expect any parameters. It creates an instance of the class with
76- > the default constructor and then assigns the key-value pairs to the instance
77- > properties. If any key in the hashtable isn't a valid property name,
78- > PowerShell raises an error.
74+ > The ` <convertable-value-type> ` syntax only works for classes that have a
75+ > default constructor that doesn't expect any parameters. It creates an
76+ > instance of the class with the default constructor and then uses runtime type
77+ > conversion to assign the values provided.
7978
8079## Examples
8180
9998Fabrikam, Inc.
10099```
101100
102- ### Example 2 - Class with instance members
101+ ### Example 2 - Using instantiation syntax
102+
103+ This example defines a ** Book** class with several properties, but no
104+ constructor.
105+
106+ ``` powershell
107+ class Book {
108+ # Class properties
109+ [string] $Title
110+ [string] $Author
111+ [string] $Synopsis
112+ [string] $Publisher
113+ [datetime] $PublishDate
114+ [int] $PageCount
115+ [string[]] $Tags
116+ }
117+ ```
118+
119+ The following example shows how the default constructor can assign the values
120+ from a compatible value using type coercion. In this example, a hashtable is
121+ used to provide the property values.
122+
123+ ``` powershell
124+ $Book1 = [Book] @{
125+ Title = '1984'
126+ Author = 'George Orwell'
127+ Synopsis = ''
128+ Publisher = 'Secker & Warburg'
129+ PublishDate = '1949-06-08'
130+ PageCount = 328
131+ Tags = @('Dystopian', 'Political Fiction', 'Social Science Fiction')
132+ }
133+ $Book1
134+ ```
135+
136+ ``` Output
137+ Title : 1984
138+ Author : George Orwell
139+ Synopsis :
140+ Publisher : Secker & Warburg
141+ PublishDate : 6/8/1949 12:00:00 AM
142+ PageCount : 328
143+ Tags : {Dystopian, Political Fiction, Social Science Fiction}
144+ ```
145+
146+ The key-value pairs of the hashtable are assigned to the instance properties.
147+ If any key in the hashtable isn't a valid property name, instantiation fails.
148+
149+ In this example, an array is used to provide the values for the generic list.
150+
151+ ``` powershell
152+ $List = [System.Collections.Generic.List[int]] @(42, 43)
153+ $List
154+ ```
155+
156+ ``` Output
157+ 42
158+ 43
159+ ```
160+
161+ ### Example 3 - Class with instance members
103162
104163This example defines a ** Book** class with several properties, constructors,
105164and methods. Every defined member is an _ instance_ member, not a static member.
@@ -190,12 +249,12 @@ It takes 10 hours and 20 minutes to read The Hobbit by J.R.R. Tolkien (1937),
190249which was published 86 years ago.
191250```
192251
193- ### Example 3 - Class with static members
252+ ### Example 4 - Class with static members
194253
195- The ** BookList** class in this example builds on the ** Book** class in example
196- 2 . While the ** BookList** class can't be marked static itself, the
197- implementation only defines the ** Books** static property and a set of static
198- methods for managing that property.
254+ The ** BookList** class in this example builds on the ** Book** class in the
255+ previous example . While the ** BookList** class can't be marked static itself,
256+ the implementation only defines the ** Books** static property and a set of
257+ static methods for managing that property.
199258
200259``` powershell
201260class BookList {
@@ -369,7 +428,7 @@ Properties are variables declared in the class scope. A property can be of any
369428built-in type or an instance of another class. Classes can have zero or more
370429properties. Classes don't have a maximum property count.
371430
372- For more information, see [ about_Classes_Properties] [ 01 ] .
431+ For more information, see [ about_Classes_Properties] [ 10 ] .
373432
374433## Class methods
375434
@@ -379,7 +438,7 @@ method doesn't return any output, it must have the **Void** output type. If a
379438method doesn't explicitly define an output type, the method's output type is
380439** Void** .
381440
382- For more information, see [ about_Classes_Methods] [ 02 ] .
441+ For more information, see [ about_Classes_Methods] [ 05 ] .
383442
384443## Class constructors
385444
@@ -388,7 +447,7 @@ moment of creating the instance of the class. Constructors have the same name
388447as the class. Constructors might have parameters, to initialize the data
389448members of the new object.
390449
391- For more information, see [ about_Classes_Constructors] [ 03 ] .
450+ For more information, see [ about_Classes_Constructors] [ 01 ] .
392451
393452## Hidden keyword
394453
@@ -417,11 +476,11 @@ Hidden class members are:
417476> When you hide any constructor, the ` new() ` option is removed from
418477> IntelliSense and completion results.
419478
420- For more information about the keyword, see [ about_Hidden] [ 04 ] . For more
421- information about hidden properties, see [ about_Classes_Properties] [ 05 ] . For
479+ For more information about the keyword, see [ about_Hidden] [ 12 ] . For more
480+ information about hidden properties, see [ about_Classes_Properties] [ 09 ] . For
422481more information about hidden methods, see [ about_Classes_Methods] [ 06 ] . For
423482more information about hidden constructors, see
424- [ about_Classes_Constructors] [ 07 ] .
483+ [ about_Classes_Constructors] [ 02 ] .
425484
426485## Static keyword
427486
@@ -435,9 +494,9 @@ available always. All static properties live for the entire session span.
435494The ` static ` keyword only applies to class members, not a class itself.
436495
437496For more information about static properties, see
438- [ about_Classes_Properties] [ 08 ] . For more information about static methods, see
439- [ about_Classes_Methods] [ 09 ] . For more information about static constructors,
440- see [ about_Classes_Constructors] [ 10 ] .
497+ [ about_Classes_Properties] [ 10 ] . For more information about static methods, see
498+ [ about_Classes_Methods] [ 07 ] . For more information about static constructors,
499+ see [ about_Classes_Constructors] [ 03 ] .
441500
442501## Inheritance in PowerShell classes
443502
@@ -453,8 +512,7 @@ inherits from an interface must implement that contract. When it does, the
453512class can be used like any other class implementing that interface.
454513
455514For more information about deriving classes that inherit from a base class or
456- implement interfaces, see
457- [ about_Classes_Inheritance] [ 11 ] .
515+ implement interfaces, see [ about_Classes_Inheritance] [ 04 ] .
458516
459517## Export classes with type accelerators
460518
@@ -540,7 +598,7 @@ consistently import classes defined in nested modules or classes defined in
540598scripts that are dot-sourced into the root module. Define classes that you want
541599to be available to users outside of the module directly in the root module.
542600
543- For more information about the ` using ` statement, see [ about_Using] [ 12 ] .
601+ For more information about the ` using ` statement, see [ about_Using] [ 15 ] .
544602
545603## Load newly changed code during development
546604
@@ -567,7 +625,7 @@ parameters can't be used with class members. The **PSReference** class was
567625designed to support COM objects. COM objects have cases where you need to pass
568626a value in by reference.
569627
570- For more information, see [ PSReference Class] [ 13 ] .
628+ For more information, see [ PSReference Class] [ 16 ] .
571629
572630## Limitations
573631
@@ -681,30 +739,30 @@ workaround for those limitations, if any.
681739
682740## See also
683741
684- - [ about_Classes_Constructors] [ 03 ]
685- - [ about_Classes_Inheritance] [ 11 ]
686- - [ about_Classes_Methods] [ 02 ]
687- - [ about_Classes_Properties] [ 01 ]
688- - [ about_Enum] [ 14 ]
689- - [ about_Hidden] [ 04 ]
690- - [ about_Language_Keywords] [ 15 ]
691- - [ about_Methods] [ 16 ]
692- - [ about_Using] [ 12 ]
742+ - [ about_Classes_Constructors] [ 01 ]
743+ - [ about_Classes_Inheritance] [ 04 ]
744+ - [ about_Classes_Methods] [ 05 ]
745+ - [ about_Classes_Properties] [ 08 ]
746+ - [ about_Enum] [ 11 ]
747+ - [ about_Hidden] [ 12 ]
748+ - [ about_Language_Keywords] [ 13 ]
749+ - [ about_Methods] [ 14 ]
750+ - [ about_Using] [ 15 ]
693751
694752<!-- link references -->
695- [ 01 ] : about_Classes_Properties .md
696- [ 02 ] : about_Classes_Methods .md
697- [ 03 ] : about_Classes_Constructors.md
698- [ 04 ] : about_Hidden .md
699- [ 05 ] : about_Classes_Properties .md#hidden-properties
753+ [ 01 ] : about_Classes_Constructors .md
754+ [ 02 ] : about_Classes_Constructors .md#hidden-constructors
755+ [ 03 ] : about_Classes_Constructors.md#static-constructors
756+ [ 04 ] : about_Classes_Inheritance .md
757+ [ 05 ] : about_Classes_Methods .md
700758[ 06 ] : about_Classes_Methods.md#hidden-methods
701- [ 07 ] : about_Classes_Constructors .md#hidden-constructors
702- [ 08 ] : about_Classes_Properties.md#static-properties
703- [ 09 ] : about_Classes_Methods .md#static-methods
704- [ 10 ] : about_Classes_Constructors .md#static-constructors
705- [ 11 ] : about_Classes_Inheritance .md
706- [ 12 ] : about_Using .md
707- [ 13 ] : /dotnet/api/system.management.automation.psreference
708- [ 14 ] : about_Enum .md
709- [ 15 ] : about_language_keywords .md
710- [ 16 ] : about_methods.md
759+ [ 07 ] : about_Classes_Methods .md#static-methods
760+ [ 08 ] : about_Classes_Properties.md
761+ [ 09 ] : about_Classes_Properties .md#hidden-properties
762+ [ 10 ] : about_Classes_Properties .md#static-properties
763+ [ 11 ] : about_Enum .md
764+ [ 12 ] : about_Hidden .md
765+ [ 13 ] : about_language_keywords.md
766+ [ 14 ] : about_methods .md
767+ [ 15 ] : about_Using .md
768+ [ 16 ] : xref:System.Management.Automation.PSReference
0 commit comments