Skip to content

Commit 472569c

Browse files
committed
Update description of instantiation syntax
1 parent fb4fea7 commit 472569c

4 files changed

Lines changed: 275 additions & 39 deletions

File tree

reference/5.1/Microsoft.PowerShell.Core/About/about_Classes.md

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes how you can use classes to create your own custom types.
33
Locale: en-US
4-
ms.date: 01/19/2024
4+
ms.date: 01/22/2026
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_classes?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: 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

@@ -99,7 +98,67 @@ Brand
9998
Fabrikam, 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+
of a compatible using type coercion. In this example, a hashtable is used to
121+
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

104163
This example defines a **Book** class with several properties, constructors,
105164
and methods. Every defined member is an _instance_ member, not a static member.
@@ -190,7 +249,7 @@ It takes 10 hours and 20 minutes to read The Hobbit by J.R.R. Tolkien (1937),
190249
which was published 86 years ago.
191250
```
192251

193-
### Example 3 - Class with static members
252+
### Example 4 - Class with static members
194253

195254
The **BookList** class in this example builds on the **Book** class in example
196255
2. While the **BookList** class can't be marked static itself, the

reference/7.4/Microsoft.PowerShell.Core/About/about_Classes.md

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes how you can use classes to create your own custom types.
33
Locale: en-US
4-
ms.date: 01/23/2024
4+
ms.date: 01/22/2026
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_classes?view=powershell-7.4&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: 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

@@ -99,7 +98,67 @@ Brand
9998
Fabrikam, 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+
of a compatible using type coercion. In this example, a hashtable is used to
121+
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

104163
This example defines a **Book** class with several properties, constructors,
105164
and methods. Every defined member is an _instance_ member, not a static member.
@@ -190,7 +249,7 @@ It takes 10 hours and 20 minutes to read The Hobbit by J.R.R. Tolkien (1937),
190249
which was published 86 years ago.
191250
```
192251

193-
### Example 3 - Class with static members
252+
### Example 4 - Class with static members
194253

195254
The **BookList** class in this example builds on the **Book** class in example
196255
2. While the **BookList** class can't be marked static itself, the
@@ -360,7 +419,7 @@ Line |
360419
| Book 'The Hobbit by J.R.R. Tolkien (1937)' already in list
361420
```
362421

363-
### Example 4 - Class definition with and without Runspace affinity
422+
### Example 5 - Class definition with and without Runspace affinity
364423

365424
The `ShowRunspaceId()` method of `[UnsafeClass]` reports different thread Ids
366425
but the same runspace ID. Eventually, the session state is corrupted causing

reference/7.5/Microsoft.PowerShell.Core/About/about_Classes.md

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes how you can use classes to create your own custom types.
33
Locale: en-US
4-
ms.date: 01/23/2024
4+
ms.date: 01/22/2026
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_classes?view=powershell-7.5&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: 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

@@ -99,7 +98,67 @@ Brand
9998
Fabrikam, 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+
of a compatible using type coercion. In this example, a hashtable is used to
121+
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

104163
This example defines a **Book** class with several properties, constructors,
105164
and methods. Every defined member is an _instance_ member, not a static member.
@@ -190,7 +249,7 @@ It takes 10 hours and 20 minutes to read The Hobbit by J.R.R. Tolkien (1937),
190249
which was published 86 years ago.
191250
```
192251

193-
### Example 3 - Class with static members
252+
### Example 4 - Class with static members
194253

195254
The **BookList** class in this example builds on the **Book** class in example
196255
2. While the **BookList** class can't be marked static itself, the
@@ -360,7 +419,7 @@ Line |
360419
| Book 'The Hobbit by J.R.R. Tolkien (1937)' already in list
361420
```
362421

363-
### Example 4 - Class definition with and without Runspace affinity
422+
### Example 5 - Class definition with and without Runspace affinity
364423

365424
The `ShowRunspaceId()` method of `[UnsafeClass]` reports different thread Ids
366425
but the same runspace ID. Eventually, the session state is corrupted causing

0 commit comments

Comments
 (0)