|
| 1 | +--- |
| 2 | +ms.date: 2017-06-09 |
| 3 | +schema: 2.0.0 |
| 4 | +locale: en-us |
| 5 | +keywords: powershell,cmdlet |
| 6 | +online version: |
| 7 | +external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml |
| 8 | +title: ConvertTo-Clixml |
| 9 | +--- |
| 10 | + |
| 11 | +# ConvertTo-Clixml |
| 12 | + |
| 13 | +## SYNOPSIS |
| 14 | +Converts objects to an XML-based representation and returns that as a string. |
| 15 | + |
| 16 | +## SYNTAX |
| 17 | + |
| 18 | +``` |
| 19 | +ConvertTo-Clixml [-Depth <Int32>] -InputObject <PSObject> [-Encoding <String>] [<CommonParameters>] |
| 20 | +``` |
| 21 | + |
| 22 | +## DESCRIPTION |
| 23 | +The **ConvertTo-CliXml** cmdlet creates XML-based representations of objects and returns them as strings. |
| 24 | +You can then use the **ConvertFrom-Clixml** cmdlet to re-create the saved objects based on the contents of the strings. |
| 25 | + |
| 26 | +A valuable use of **ConvertTo-CliXml** is to serialize credentials and secure strings securely as XML. |
| 27 | +For an example of how to do this, see Example 3. |
| 28 | + |
| 29 | +## EXAMPLES |
| 30 | + |
| 31 | +### Example 1: Convert a string to an XML representation |
| 32 | + |
| 33 | +```powershell |
| 34 | +"This is a test" | ConvertTo-Clixml |
| 35 | +``` |
| 36 | + |
| 37 | +This command returns a string with am XML-based representation of the string object with a value of "This is a test". |
| 38 | + |
| 39 | +### Example 2: Convert an object to an XML-based representation |
| 40 | + |
| 41 | +```powershell |
| 42 | +$FileaclString = Get-Acl C:\test.txt | ConvertTo-Clixml |
| 43 | +Fileacl = ConvertFrom-Clixml $FileaclString |
| 44 | +``` |
| 45 | + |
| 46 | +This example shows how to convert an object to an XML string and then create an object by converting the XML from the string. |
| 47 | + |
| 48 | +The first command uses the Get-Acl cmdlet to get the security descriptor of the Test.txt file. |
| 49 | +It uses a pipeline operator to pass the security descriptor to **ConvertTo-Clixml**, which converts the object to an XML-based representation and returns that as a string. |
| 50 | +Then, it saves the string in the $FileAclString variable. |
| 51 | + |
| 52 | +The second command uses the ConvertFrom-Clixml cmdlet to create an object from the XML in the $FileAclString variable. |
| 53 | +Then, it saves the object in the $FileAcl variable. |
| 54 | + |
| 55 | +### Example 3: Convert an encrypted credential object |
| 56 | + |
| 57 | +```powershell |
| 58 | +$CredXml = $Credential | ConvertTo-Clixml |
| 59 | +$Credential = ConvertFrom-CliXml $CredXml |
| 60 | +``` |
| 61 | + |
| 62 | +The **ConvertTo-CliXml** cmdlet encrypts credential objects by using the |
| 63 | +[Windows Data Protection API](http://msdn.microsoft.com/library/windows/apps/xaml/hh464970.aspx). |
| 64 | +This ensures that only your user account can decrypt the contents of the credential object. |
| 65 | + |
| 66 | +In this example, given a credential that you've stored in the $Credential variable by running the Get-Credential cmdlet, you can run the **ConvertTo-CliXml** cmdlet to serialize the credential to a string. |
| 67 | + |
| 68 | +To deserialize the credential later, run the second command. |
| 69 | +This time, you are running ConvertFrom-Clixml to import the secured credential object into your script. |
| 70 | +This eliminates the risk of exposing plain-text passwords in your script. |
| 71 | + |
| 72 | +## PARAMETERS |
| 73 | + |
| 74 | +### -Depth |
| 75 | +Specifies how many levels of contained objects are included in the XML representation. |
| 76 | +The default value is 2. |
| 77 | + |
| 78 | +The default value can be overridden for the object type in the Types.ps1xml files. |
| 79 | +For more information, see about_Types.ps1xml. |
| 80 | + |
| 81 | +```yaml |
| 82 | +Type: Int32 |
| 83 | +Parameter Sets: (All) |
| 84 | +Aliases: |
| 85 | + |
| 86 | +Required: False |
| 87 | +Position: Named |
| 88 | +Default value: None |
| 89 | +Accept pipeline input: False |
| 90 | +Accept wildcard characters: False |
| 91 | +``` |
| 92 | +
|
| 93 | +### -Encoding |
| 94 | +Specifies the type of encoding for the target file. |
| 95 | +The acceptable values for this parameter are: |
| 96 | +
|
| 97 | +- ASCII |
| 98 | +- UTF8 |
| 99 | +- UTF7 |
| 100 | +- UTF32 |
| 101 | +- Unicode |
| 102 | +- BigEndianUnicode |
| 103 | +- Default |
| 104 | +- OEM |
| 105 | +
|
| 106 | +The default value is Unicode. |
| 107 | +
|
| 108 | +```yaml |
| 109 | +Type: String |
| 110 | +Parameter Sets: (All) |
| 111 | +Aliases: |
| 112 | +Accepted values: Unicode, UTF7, UTF8, ASCII, UTF32, BigEndianUnicode, Default, OEM |
| 113 | + |
| 114 | +Required: False |
| 115 | +Position: Named |
| 116 | +Default value: None |
| 117 | +Accept pipeline input: False |
| 118 | +Accept wildcard characters: False |
| 119 | +``` |
| 120 | +
|
| 121 | +### -InputObject |
| 122 | +Specifies the object to be converted. |
| 123 | +Enter a variable that contains the objects, or type a command or expression that gets the objects. |
| 124 | +You can also pipe objects to **ConvertTo-Clixml**. |
| 125 | +
|
| 126 | +```yaml |
| 127 | +Type: PSObject |
| 128 | +Parameter Sets: (All) |
| 129 | +Aliases: |
| 130 | + |
| 131 | +Required: True |
| 132 | +Position: Named |
| 133 | +Default value: None |
| 134 | +Accept pipeline input: True (ByValue) |
| 135 | +Accept wildcard characters: False |
| 136 | +``` |
| 137 | +
|
| 138 | +### CommonParameters |
| 139 | +This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](../../Microsoft.PowerShell.Core/About/about_CommonParameters.md). |
| 140 | +
|
| 141 | +## INPUTS |
| 142 | +
|
| 143 | +### System.Management.Automation.PSObject |
| 144 | +You can pipe any object to **ConvertTo-Clixml**. |
| 145 | +
|
| 146 | +## OUTPUTS |
| 147 | +
|
| 148 | +### System.String |
| 149 | +The XML-based representation is returned as a collection of strings. |
| 150 | +
|
| 151 | +## NOTES |
| 152 | +
|
| 153 | +## RELATED LINKS |
| 154 | +
|
| 155 | +[Use PowerShell to Pass Credentials to Legacy Systems](http://blogs.technet.com/b/heyscriptingguy/archive/2011/06/05/use-powershell-to-pass-credentials-to-legacy-systems.aspx) |
| 156 | +
|
| 157 | +[Securely Store Credentials on Disk](http://powershellcookbook.com/recipe/PukO/securely-store-credentials-on-disk) |
| 158 | +
|
| 159 | +[ConvertTo-Json](ConvertTo-Json.md) |
| 160 | +
|
| 161 | +[ConvertTo-Xml](ConvertTo-Xml.md) |
| 162 | +
|
| 163 | +[ConvertTo-Csv](ConvertTo-Csv.md) |
| 164 | +
|
| 165 | +[Export-Clixml](Export-Clixml.md) |
| 166 | +
|
| 167 | +[Import-Clixml](Import-Clixml.md) |
| 168 | +
|
| 169 | +[ConvertFrom-Clixml](ConvertFrom-Clixml.md) |
0 commit comments