Skip to content

Commit 0d5f2d2

Browse files
authored
Fixes #10843 - Explain deserialized object and add example (#10844)
* Explain deserialized object and add example * Copy changes and fix bookmark
1 parent 22b5163 commit 0d5f2d2

File tree

15 files changed

+451
-108
lines changed

15 files changed

+451
-108
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes how to interpret and format the output of remote commands.
33
Locale: en-US
4-
ms.date: 07/03/2023
4+
ms.date: 01/31/2024
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_remote_output?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about Remote Output
@@ -95,10 +95,9 @@ Monday, July 21, 2008 7:16:58 PM Server02
9595
When you run remote commands that generate output, the command output is
9696
transmitted across the network back to the local computer.
9797

98-
Because most live Microsoft .NET objects, like the objects that PowerShell
99-
cmdlets return, can't be transmitted over the network. The live objects are
100-
_serialized_ or converted into XML representations of the object and its
101-
properties. Then, PowerShell transmits the serialized object across the
98+
Since live .NET objects can't be transmitted over the network, the live objects
99+
are _serialized_ or converted into XML representations of the object and its
100+
properties. PowerShell transmits the serialized object across the
102101
network.
103102

104103
On the local computer, PowerShell receives the serialized object and
@@ -139,7 +138,7 @@ determines the order in which PowerShell connects to the remote computers.
139138
However, the results appear in the order that the data is received from the
140139
remote computers.
141140

142-
You can use the `Sort-Object` cmdlet to sort the results on the
141+
You can use the `Sort-Object` cmdlet to sort the results on on the
143142
**PSComputerName**. When you any other property of the object, the results from
144143
different computers are interspersed interleaved in the output
145144

reference/5.1/Microsoft.PowerShell.Utility/Export-Clixml.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Utility
5-
ms.date: 12/12/2022
5+
ms.date: 01/31/2024
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/export-clixml?view=powershell-5.1&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Export-Clixml
@@ -31,10 +31,10 @@ Export-Clixml -LiteralPath <String> -InputObject <PSObject> [-Depth <Int32>] [-F
3131

3232
## DESCRIPTION
3333

34-
The `Export-Clixml` cmdlet creates a Common Language Infrastructure (CLI) XML-based representation
35-
of an object or objects and stores it in a file. You can then use the `Import-Clixml` cmdlet to
36-
recreate the saved object based on the contents of that file.
37-
For more information about CLI, see [Language independence](/dotnet/standard/language-independence).
34+
The `Export-Clixml` cmdlet serialized an object into a Common Language Infrastructure (CLI)
35+
XML-based representation stores it in a file. You can then use the `Import-Clixml` cmdlet to
36+
recreate the saved object based on the contents of that file. For more information about CLI, see
37+
[Language independence](/dotnet/standard/language-independence).
3838

3939
This cmdlet is similar to `ConvertTo-Xml`, except that `Export-Clixml` stores the resulting XML in a
4040
file. `ConvertTo-XML` returns the XML, so you can continue to process it in PowerShell.
@@ -53,7 +53,7 @@ string **This is a test**.
5353
"This is a test" | Export-Clixml -Path .\sample.xml
5454
```
5555

56-
The string **This is a test** is sent down the pipeline. `Export-Clixml` uses the **Path** parameter
56+
The string `This is a test` is sent down the pipeline. `Export-Clixml` uses the **Path** parameter
5757
to create an XML file named `sample.xml` in the current directory.
5858

5959
### Example 2: Export an object to an XML file
@@ -80,7 +80,8 @@ In this example, given a credential that you've stored in the `$Credential` vari
8080

8181
> [!IMPORTANT]
8282
> `Export-Clixml` only exports encrypted credentials on Windows. On non-Windows operating systems
83-
> such as macOS and Linux, credentials are exported in plain text.
83+
> such as macOS and Linux, credentials are exported as a plain text stored as a Unicode character
84+
> array. This provides some obfuscation but does not provide encryption.
8485
8586
```powershell
8687
$Credxmlpath = Join-Path (Split-Path $Profile) TestScript.ps1.credential
@@ -89,10 +90,10 @@ $Credxmlpath = Join-Path (Split-Path $Profile) TestScript.ps1.credential
8990
$Credential = Import-Clixml $Credxmlpath
9091
```
9192

92-
The `Export-Clixml` cmdlet encrypts credential objects by using the Windows [Data Protection API](/previous-versions/windows/apps/hh464970(v=win.10)).
93-
The encryption ensures that only your user account on only that computer can decrypt the contents of
94-
the credential object. The exported `CLIXML` file can't be used on a different computer or by a
95-
different user.
93+
The `Export-Clixml` cmdlet encrypts credential objects by using the Windows
94+
[Data Protection API](/previous-versions/windows/apps/hh464970(v=win.10)). The encryption ensures
95+
that only your user account on only that computer can decrypt the contents of the credential object.
96+
The exported `CLIXML` file can't be used on a different computer or by a different user.
9697

9798
In the example, the file in which the credential is stored is represented by
9899
`TestScript.ps1.credential`. Replace **TestScript** with the name of the script with which you're
@@ -279,7 +280,8 @@ Accept wildcard characters: False
279280

280281
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable,
281282
-InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose,
282-
-WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216).
283+
-WarningAction, and -WarningVariable. For more information, see
284+
[about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216).
283285

284286
## INPUTS
285287

reference/5.1/Microsoft.PowerShell.Utility/Import-Clixml.md

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Utility
5-
ms.date: 12/12/2022
5+
ms.date: 01/31/2024
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/import-clixml?view=powershell-5.1&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Import-Clixml
@@ -31,16 +31,26 @@ Import-Clixml -LiteralPath <String[]> [-IncludeTotalCount] [-Skip <UInt64>] [-Fi
3131

3232
## DESCRIPTION
3333

34-
The `Import-Clixml` cmdlet imports a Common Language Infrastructure (CLI) XML file with data that
35-
represents Microsoft .NET Framework objects and creates the PowerShell objects. For more information
36-
about CLI, see [Language independence](/dotnet/standard/language-independence).
34+
The `Import-Clixml` cmdlet imports objects that have been serialized into a Common Language
35+
Infrastructure (CLI) XML file. A valuable use of `Import-Clixml` on Windows computers is to import
36+
credentials and secure strings that were exported as secure XML using `Export-Clixml`.
37+
[Example #2](#example-2-import-a-secure-credential-object) shows how to use `Import-Clixml` to
38+
import a secure credential object.
3739

38-
A valuable use of `Import-Clixml` on Windows computers is to import credentials and secure strings
39-
that were exported as secure XML using `Export-Clixml`. For an example, see Example 2.
40+
The CLIXML data is deserialized back into PowerShell objects. However, the deserialized objects
41+
aren't a live objects. They are a snapshot of the objects at the time of serialization. The
42+
deserialized objects include properties but no methods.
43+
44+
The **TypeNames** property contains the original type name prefixed with `Deserialized`.
45+
[Example #3](#example-3-inspect-the-typenames-property-of-a-deserialized-object) show the
46+
**TypeNames** property of a deserialized object.
4047

4148
`Import-Clixml` uses the byte-order-mark (BOM) to detect the encoding format of the file. If the
4249
file has no BOM, it assumes the encoding is UTF8.
4350

51+
For more information about CLI, see
52+
[Language independence](/dotnet/standard/language-independence).
53+
4454
## EXAMPLES
4555

4656
### Example 1: Import a serialized file and recreate an object
@@ -70,9 +80,10 @@ $Credxmlpath = Join-Path (Split-Path $Profile) TestScript.ps1.credential
7080
$Credential = Import-Clixml $Credxmlpath
7181
```
7282

73-
The `Export-Clixml` cmdlet encrypts credential objects by using the Windows [Data Protection API](/previous-versions/windows/apps/hh464970(v=win.10)).
74-
The encryption ensures that only your user account can decrypt the contents of the credential
75-
object. The exported `CLIXML` file can't be used on a different computer or by a different user.
83+
The `Export-Clixml` cmdlet encrypts credential objects by using the Windows
84+
[Data Protection API](/previous-versions/windows/apps/hh464970(v=win.10)). The encryption ensures
85+
that only your user account can decrypt the contents of the credential object. The exported `CLIXML`
86+
file can't be used on a different computer or by a different user.
7687

7788
In the example, the file in which the credential is stored is represented by
7889
`TestScript.ps1.credential`. Replace **TestScript** with the name of the script with which you're
@@ -85,6 +96,62 @@ To import the credential automatically into your script, run the final two comma
8596
`Import-Clixml` to import the secured credential object into your script. This import eliminates the
8697
risk of exposing plain-text passwords in your script.
8798

99+
### Example 3: Inspect the TypeNames property of a deserialized object
100+
101+
This example shows importing an object stored as CLIXML data. The data is deserialized back into a
102+
PowerShell object. However, the deserialized object aren't a live objects. They are a snapshot of
103+
the objects at the time of serialization. The deserialized objects include properties but no
104+
methods.
105+
106+
```powershell
107+
$original = [pscustomobject] @{
108+
Timestamp = Get-Date
109+
Label = 'Meeting event'
110+
}
111+
$original | Add-Member -MemberType ScriptMethod -Name GetDisplay -Value {
112+
'{0:yyyy-MM-dd HH:mm} {1}' -f $this.Timestamp, $this.Label
113+
}
114+
$original | Get-Member -MemberType ScriptMethod
115+
```
116+
117+
```Output
118+
TypeName: System.Management.Automation.PSCustomObject
119+
120+
Name MemberType Definition
121+
---- ---------- ----------
122+
Equals Method bool Equals(System.Object obj)
123+
GetHashCode Method int GetHashCode()
124+
GetType Method type GetType()
125+
ToString Method string ToString()
126+
Label NoteProperty string Label=Meeting event
127+
Timestamp NoteProperty System.DateTime Timestamp=1/31/2024 2:27:59 PM
128+
GetDisplay ScriptMethod System.Object GetDisplay();
129+
```
130+
131+
```powershell
132+
$original | Export-Clixml -Path event.clixml
133+
$deserialized = Import-CliXml -Path event.clixml
134+
$deserialized | Get-Member
135+
```
136+
137+
```Output
138+
TypeName: Deserialized.System.Management.Automation.PSCustomObject
139+
140+
Name MemberType Definition
141+
---- ---------- ----------
142+
Equals Method bool Equals(System.Object obj)
143+
GetHashCode Method int GetHashCode()
144+
GetType Method type GetType()
145+
ToString Method string ToString()
146+
Label NoteProperty string Label=Meeting event
147+
Timestamp NoteProperty System.DateTime Timestamp=1/31/2024 2:27:59 PM
148+
```
149+
150+
Note that the type of the object in `$original` is **System.Management.Automation.PSCustomObject**,
151+
but the type of the object in `$deserialized` is
152+
**Deserialized.System.Management.Automation.PSCustomObject**. Also, the `GetDisplay()` method is
153+
missing from the deserialized object.
154+
88155
## PARAMETERS
89156

90157
### -First
@@ -180,7 +247,8 @@ Accept wildcard characters: False
180247

181248
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable,
182249
-InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose,
183-
-WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216).
250+
-WarningAction, and -WarningVariable. For more information, see
251+
[about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216).
184252

185253
## INPUTS
186254

reference/7.2/Microsoft.PowerShell.Core/About/about_Remote_Output.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes how to interpret and format the output of remote commands.
33
Locale: en-US
4-
ms.date: 07/03/2023
4+
ms.date: 01/31/2024
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_remote_output?view=powershell-7.2&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about Remote Output
@@ -95,10 +95,9 @@ Monday, July 21, 2008 7:16:58 PM Server02
9595
When you run remote commands that generate output, the command output is
9696
transmitted across the network back to the local computer.
9797

98-
Because most live Microsoft .NET objects, like the objects that PowerShell
99-
cmdlets return, can't be transmitted over the network. The live objects are
100-
_serialized_ or converted into XML representations of the object and its
101-
properties. Then, PowerShell transmits the serialized object across the
98+
Since live .NET objects can't be transmitted over the network, the live objects
99+
are _serialized_ or converted into XML representations of the object and its
100+
properties. PowerShell transmits the serialized object across the
102101
network.
103102

104103
On the local computer, PowerShell receives the serialized object and

reference/7.2/Microsoft.PowerShell.Utility/Export-Clixml.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Utility
5-
ms.date: 12/12/2022
5+
ms.date: 01/31/2024
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/export-clixml?view=powershell-7.2&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Export-Clixml
@@ -333,7 +333,8 @@ Accept wildcard characters: False
333333

334334
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable,
335335
-InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose,
336-
-WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216).
336+
-WarningAction, and -WarningVariable. For more information, see
337+
[about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216).
337338

338339
## INPUTS
339340

reference/7.2/Microsoft.PowerShell.Utility/Import-Clixml.md

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Utility
5-
ms.date: 12/12/2022
5+
ms.date: 01/31/2024
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/import-clixml?view=powershell-7.2&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Import-Clixml
@@ -31,16 +31,26 @@ Import-Clixml -LiteralPath <String[]> [-IncludeTotalCount] [-Skip <UInt64>] [-Fi
3131

3232
## DESCRIPTION
3333

34-
The `Import-Clixml` cmdlet imports a Common Language Infrastructure (CLI) XML file with data that
35-
represents Microsoft .NET Framework objects and creates the PowerShell objects. For more information
36-
about CLI, see [Language independence](/dotnet/standard/language-independence).
34+
The `Import-Clixml` cmdlet imports objects that have been serialized into a Common Language
35+
Infrastructure (CLI) XML file. A valuable use of `Import-Clixml` on Windows computers is to import
36+
credentials and secure strings that were exported as secure XML using `Export-Clixml`.
37+
[Example #2](#example-2-import-a-secure-credential-object) shows how to use `Import-Clixml` to
38+
import a secure credential object.
3739

38-
A valuable use of `Import-Clixml` on Windows computers is to import credentials and secure strings
39-
that were exported as secure XML using `Export-Clixml`. For an example, see Example 2.
40+
The CLIXML data is deserialized back into PowerShell objects. However, the deserialized objects
41+
aren't a live objects. They are a snapshot of the objects at the time of serialization. The
42+
deserialized objects include properties but no methods.
43+
44+
The **TypeNames** property contains the original type name prefixed with `Deserialized`.
45+
[Example #3](#example-3-inspect-the-typenames-property-of-a-deserialized-object) show the
46+
**TypeNames** property of a deserialized object.
4047

4148
`Import-Clixml` uses the byte-order-mark (BOM) to detect the encoding format of the file. If the
4249
file has no BOM, it assumes the encoding is UTF8.
4350

51+
For more information about CLI, see
52+
[Language independence](/dotnet/standard/language-independence).
53+
4454
## EXAMPLES
4555

4656
### Example 1: Import a serialized file and recreate an object
@@ -70,9 +80,10 @@ $Credxmlpath = Join-Path (Split-Path $Profile) TestScript.ps1.credential
7080
$Credential = Import-Clixml $Credxmlpath
7181
```
7282

73-
The `Export-Clixml` cmdlet encrypts credential objects by using the Windows [Data Protection API](/previous-versions/windows/apps/hh464970(v=win.10)).
74-
The encryption ensures that only your user account can decrypt the contents of the credential
75-
object. The exported `CLIXML` file can't be used on a different computer or by a different user.
83+
The `Export-Clixml` cmdlet encrypts credential objects by using the Windows
84+
[Data Protection API](/previous-versions/windows/apps/hh464970(v=win.10)). The encryption ensures
85+
that only your user account can decrypt the contents of the credential object. The exported `CLIXML`
86+
file can't be used on a different computer or by a different user.
7687

7788
In the example, the file in which the credential is stored is represented by
7889
`TestScript.ps1.credential`. Replace **TestScript** with the name of the script with which you're
@@ -85,6 +96,62 @@ To import the credential automatically into your script, run the final two comma
8596
`Import-Clixml` to import the secured credential object into your script. This import eliminates the
8697
risk of exposing plain-text passwords in your script.
8798

99+
### Example 3: Inspect the TypeNames property of a deserialized object
100+
101+
This example shows importing an object stored as CLIXML data. The data is deserialized back into a
102+
PowerShell object. However, the deserialized object aren't a live objects. They are a snapshot of
103+
the objects at the time of serialization. The deserialized objects include properties but no
104+
methods.
105+
106+
```powershell
107+
$original = [pscustomobject] @{
108+
Timestamp = Get-Date
109+
Label = 'Meeting event'
110+
}
111+
$original | Add-Member -MemberType ScriptMethod -Name GetDisplay -Value {
112+
'{0:yyyy-MM-dd HH:mm} {1}' -f $this.Timestamp, $this.Label
113+
}
114+
$original | Get-Member -MemberType ScriptMethod
115+
```
116+
117+
```Output
118+
TypeName: System.Management.Automation.PSCustomObject
119+
120+
Name MemberType Definition
121+
---- ---------- ----------
122+
Equals Method bool Equals(System.Object obj)
123+
GetHashCode Method int GetHashCode()
124+
GetType Method type GetType()
125+
ToString Method string ToString()
126+
Label NoteProperty string Label=Meeting event
127+
Timestamp NoteProperty System.DateTime Timestamp=1/31/2024 2:27:59 PM
128+
GetDisplay ScriptMethod System.Object GetDisplay();
129+
```
130+
131+
```powershell
132+
$original | Export-Clixml -Path event.clixml
133+
$deserialized = Import-CliXml -Path event.clixml
134+
$deserialized | Get-Member
135+
```
136+
137+
```Output
138+
TypeName: Deserialized.System.Management.Automation.PSCustomObject
139+
140+
Name MemberType Definition
141+
---- ---------- ----------
142+
Equals Method bool Equals(System.Object obj)
143+
GetHashCode Method int GetHashCode()
144+
GetType Method type GetType()
145+
ToString Method string ToString()
146+
Label NoteProperty string Label=Meeting event
147+
Timestamp NoteProperty System.DateTime Timestamp=1/31/2024 2:27:59 PM
148+
```
149+
150+
Note that the type of the object in `$original` is **System.Management.Automation.PSCustomObject**,
151+
but the type of the object in `$deserialized` is
152+
**Deserialized.System.Management.Automation.PSCustomObject**. Also, the `GetDisplay()` method is
153+
missing from the deserialized object.
154+
88155
## PARAMETERS
89156

90157
### -First
@@ -180,7 +247,8 @@ Accept wildcard characters: False
180247

181248
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable,
182249
-InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose,
183-
-WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216).
250+
-WarningAction, and -WarningVariable. For more information, see
251+
[about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216).
184252

185253
## INPUTS
186254

0 commit comments

Comments
 (0)