ConvertFrom-StringData (Microsoft.PowerShell.Utility) - PowerShell (2024)

  • Reference
Module:
Microsoft.PowerShell.Utility

Converts a string containing one or more key and value pairs to a hash table.

Syntax

ConvertFrom-StringData [-StringData] <String> [[-Delimiter] <Char>] [<CommonParameters>]

Description

The ConvertFrom-StringData cmdlet converts a string that contains one or more key and value pairsinto a hash table. Because each key-value pair must be on a separate line, here-strings are oftenused as the input format. By default, the key must be separated from the value by an equalssign (=) character.

The ConvertFrom-StringData cmdlet is considered to be a safe cmdlet that can be used in theDATA section of a script or function. When used in a DATA section, the contents of thestring must conform to the rules for a DATA section. For more information, seeabout_Data_Sections.

ConvertFrom-StringData supports escape character sequences that are allowed by conventionalmachine translation tools. That is, the cmdlet can interpret backslashes (\) as escape charactersin the string data by using theRegex.Unescape Method, instead of thePowerShell backtick character (`) that would normally signal the end of a line in a script.Inside the here-string, the backtick character does not work. You can also preserve a literalbackslash in your results by escaping it with a preceding backslash, like this: \\. Unescapedbackslash characters, such as those that are commonly used in file paths, can render as illegalescape sequences in your results.

PowerShell 7 adds the Delimiter parameter.

Examples

Example 1: Convert a single-quoted here-string to a hash table

This example converts a single-quoted here-string of user messages into a hash table. In asingle-quoted string, values are not substituted for variables and expressions are not evaluated.The ConvertFrom-StringData cmdlet converts the value in the $Here variable to a hash table.

$Here = @'Msg1 = The string parameter is required.Msg2 = Credentials are required for this command.Msg3 = The specified variable does not exist.'@ConvertFrom-StringData -StringData $HereName Value---- -----Msg3 The specified variable does not exist.Msg2 Credentials are required for this command.Msg1 The string parameter is required.

Example 2: Convert string data using a different delimiter

This example shows how to convert string data that uses a different character as a delimiter. Inthis example the string data is using the pipe character (|) as the delimiter.

$StringData = @'color|redmodel|coupeyear|1965condition|mint'@$carData = ConvertFrom-StringData -StringData $StringData -Delimiter '|'$carDataName Value---- -----condition mintmodel coupecolor redyear 1965

Example 3: Convert a here-string containing a comment

This example converts a here-string that contains a comment and multiple key-value pairs into a hashtable.

ConvertFrom-StringData -StringData @'Name = Disks.ps1# Category is optional.Category = StorageCost = Free'@Name Value---- -----Cost FreeCategory StorageName Disks.ps1

The value of the StringData parameter is a here-string, instead of a variable that contains ahere-string. Either format is valid. The here-string includes a comment about one of the strings.ConvertFrom-StringData ignores single-line comments, but the # character must be the firstnon-whitespace character on the line. All characters on the line after the # are ignored.

Example 4: Convert a string to a hash table

This example converts a regular double-quoted string (not a here-string) into a hash table and savesit in the $A variable.

$A = ConvertFrom-StringData -StringData "Top = Red `n Bottom = Blue"$AName Value---- -----Bottom BlueTop Red

To satisfy the condition that each key-value pair must be on a separate line, the string uses thePowerShell newline character (`n) to separate the pairs.

Example 5: Use ConvertFrom-StringData in the DATA section of a script

This example shows a ConvertFrom-StringData command used in the DATA section of a script.The statements below the DATA section display the text to the user.

$TextMsgs = DATA {ConvertFrom-StringData @'Text001 = The $Notebook variable contains the name of the user's system notebook.Text002 = The $MyNotebook variable contains the name of the user's private notebook.'@}$TextMsgsName Value---- -----Text001 The $Notebook variable contains the name of the user's system notebook.Text002 The $MyNotebook variable contains the name of the user's private notebook.

Because the text includes variable names, it must be enclosed in a single-quoted string so that thevariables are interpreted literally and not expanded. Variables are not permitted in the DATAsection.

Example 6: Use the pipeline operator to pass a string

This example shows that you can use a pipeline operator (|) to send a string toConvertFrom-StringData. The value of the $Here variable is piped to ConvertFrom-StringDataand the result in the $Hash variable.

$Here = @'Msg1 = The string parameter is required.Msg2 = Credentials are required for this command.Msg3 = The specified variable does not exist.'@$Hash = $Here | ConvertFrom-StringData$HashName Value---- -----Msg3 The specified variable does not exist.Msg2 Credentials are required for this command.Msg1 The string parameter is required.

Example 7: Use escape characters to add new lines and return characters

This example shows the use of escape characters to create new lines and return characters in sourcedata. The escape sequence \n is used to create new lines within a block of text that is associatedwith a name or item in the resulting hash table.

ConvertFrom-StringData @"Vincentio = Heaven doth with us as we with torches do,\nNot light them for themselves; for if our virtues\nDid not go forth of us, 'twere all alike\nAs if we had them not.Angelo = Let there be some more test made of my metal,\nBefore so noble and so great a figure\nBe stamp'd upon it."@ | Format-ListName : AngeloValue : Let there be some more test made of my metal, Before so noble and so great a figure Be stamp'd upon it.Name : VincentioValue : Heaven doth with us as we with torches do, Not light them for themselves; for if our virtues Did not go forth of us, 'twere all alike As if we had them not.

Example 8: Use backslash escape character to correctly render a file path

This example shows how to use of the backslash escape character in the string data to allow a filepath to render correctly in the resulting ConvertFrom-StringData hash table. The double backslashensures that the literal backslash characters render correctly in the hash table output.

ConvertFrom-StringData "Message=Look in c:\\Windows\\System32"Name Value---- -----Message Look in c:\Windows\System32

Parameters

-Delimiter

The character used to separate the key from the value data in the string being converted.The default delimiter is the equals sign (=) character. This parameter was added in PowerShell 7.

Type:Char
Position:1
Default value:'='
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-StringData

Specifies the string to be converted. You can use this parameter or pipe a string toConvertFrom-StringData. The parameter name is optional.

The value of this parameter must be a string that contains one or more key-value pairs. Eachkey-value pair must be on a separate line, or each pair must be separated by newline characters(`n).

You can include comments in the string, but the comments cannot be on the same line as a key-valuepair. ConvertFrom-StringData ignores single-line comments. The # character must be the firstnon-whitespace character on the line. All characters on the line after the # are ignored. Thecomments are not included in the hash table.

A here-string is a string consisting of one or more lines. Quotation marks within the here-stringare interpreted literally as part of the string data. For more information, seeabout_Quoting_Rules.

Type:String
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

Inputs

String

You can pipe a string containing a key-value pair to this cmdlet.

Outputs

Hashtable

This cmdlet returns a hash table that it creates from the key-value pairs.

Notes

A here-string is a string consisting of one or more lines within which quotation marks areinterpreted literally.

This cmdlet can be useful in scripts that display user messages in multiple spoken languages. Youcan use the dictionary-style hash tables to isolate text strings from code, such as in resourcefiles, and to format the text strings for use in translation tools.

ConvertFrom-StringData (Microsoft.PowerShell.Utility) - PowerShell (2024)

FAQs

ConvertFrom-StringData (Microsoft.PowerShell.Utility) - PowerShell? ›

The ConvertFrom-StringData cmdlet converts a string that contains one or more key and value pairs into a hash table. Because each key-value pair must be on a separate line, here-strings are often used as the input format. By default, the key must be separated from the value by an equals sign ( = ) character.

How to convert data to string in PowerShell? ›

To convert variables to strings in PowerShell, you can use the ToString() method. This method can be called on any variable to convert its value to a string. For example, if you have an integer variable $number = 123, you can convert it to a string by using $stringNumber = $number. ToString().

How to convert object to string in PowerShell? ›

The Out-String cmdlet converts the objects that Windows PowerShell manages into an array of strings. By default, Out-String accumulates the strings and returns them as a single string, but you can use the stream parameter to direct Out-String to return one string at a time.

How to convert string array to object in PowerShell? ›

In PowerShell, you can use the ConvertFrom-Json cmdlet to convert an array of strings in JSON format into objects. Here's an example: $json = '[{"name":"John","age":30},{"name":"Jane","age":25}]' $objects = $json | ConvertFrom-Json.

What is Microsoft PowerShell Utility? ›

PowerShell is a cross-platform task automation solution made up of a command-line shell, a scripting language, and a configuration management framework.

How to convert string into datatype? ›

In Python, strings can be converted into an integer using built-in functions like 'int()', 'eval()', 'str.isdigit()', etc. However, it's important to note that the string must represent a valid integer value for the conversion to succeed. We will look at methods to change datatype from string to int in Python.

How do you convert data into string format? ›

Int to String in Java – How to Convert an Integer into a String
  1. Using the Integer. toString() method.
  2. Using the String. valueOf() method.
  3. Using the String. format() method.
  4. Using the DecimalFormat class.
Jan 5, 2023

How to convert JSON to string in PowerShell? ›

This command uses the Invoke-WebRequest cmdlet to get JSON strings from a web service and then it uses the ConvertFrom-Json cmdlet to convert JSON content to objects that can be managed in Windows PowerShell. You can also use the Invoke-RestMethod cmdlet, which automatically converts JSON content to objects.

How to convert string to CSV in PowerShell? ›

You can use the Export-Csv cmdlet to convert objects to CSV strings. Export-CSV is similar to ConvertTo-CSV , except that it saves the CSV strings to a file. The ConvertTo-CSV cmdlet has parameters to specify a delimiter other than a comma or use the current culture as the delimiter.

How do I turn an object into a string? ›

In Java, you can convert an object to a string using the toString() method, which is a method of the Object class. It returns a string that represents the object.

How do you convert a string to a char in PowerShell? ›

The best way to convert a string into an array of characters in PowerShell is by using the . ToCharArray() method. This method is part of the . NET framework and can be directly applied to any string object.

How to convert string to byte in PowerShell? ›

To convert a string to a byte array in PowerShell, use the System. Text. Encoding class with the appropriate encoding method. For example, $byteArray = [System.

How to convert a string to array? ›

Explanation:
  1. Required class is imported.
  2. Public class is defined.
  3. A string is inserted.
  4. Call toCharArray() function.
  5. Create a char array and store the result returned by toCharArray().
  6. An operation is performed on the toCharArray() function to print the result.
Jul 19, 2024

How to replace string in PowerShell? ›

To replace a substring in PowerShell, you can use the -replace operator or the Replace() method.

What is a string in PowerShell? ›

The PowerShell string is simply an object with a System. String type. It is a datatype that denotes the sequence of characters, either as a literal constant or some kind of variable. A String can be defined in PowerShell by using the single or double-quotes. Both the strings are created of the same System.

How to read PowerShell syntax? ›

You can use the Get-Help cmdlet to display the syntax of any PowerShell cmdlet. To do so, just type Get-Help followed by the cmdlet whose syntax you want to view.

How to make something a string in PowerShell? ›

In PowerShell, there are two ways to define a string: by using single quotes or double quotes. Both create the same System.

How to change data type in PowerShell? ›

Beginning in Windows PowerShell 3.0, you can use Update-TypeData to add and replace extended type data in the session without using a Types. ps1xml file. Type data that is added dynamically, that is, without a file, is added only to the current session.

How to convert int to string in shell? ›

In a shell script, you can convert an integer to a string using the echo command and command substitution with the printf command. Here's an example: bashCopy code#!/bin/bash # assign an integer value myint=42 # convert integer to string using printf and command substitution mystring=$(printf "%d" $myint) …

How do you pass a variable into a string in PowerShell? ›

PowerShell declares a variable by using the $ sign and the variable name. For example, $Services. When we declare any type (Integer, double, string, DateTime) of the variable and when we use them inside the string variable, it automatically converts that variable to the string.

Top Articles
The Netherlands Travel Guide + Road Trip Itinerary | Moon & Honey Travel
7 Ways For Entrepreneurs To Find Investors And Raise Millions
Tlc Africa Deaths 2021
Craigslist Free En Dallas Tx
Botw Royal Guard
Missed Connections Inland Empire
Ixl Elmoreco.com
Linkvertise Bypass 2023
Die Windows GDI+ (Teil 1)
Paula Deen Italian Cream Cake
2021 Lexus IS for sale - Richardson, TX - craigslist
Hair Love Salon Bradley Beach
Mary Kay Lipstick Conversion Chart PDF Form - FormsPal
State HOF Adds 25 More Players
Connect U Of M Dearborn
50 Shades Darker Movie 123Movies
Vermont Craigs List
Pretend Newlyweds Nikubou Maranoshin
Where Is The Nearest Popeyes
라이키 유출
Baja Boats For Sale On Craigslist
Gilchrist Verband - Lumedis - Ihre Schulterspezialisten
The 15 Best Sites to Watch Movies for Free (Legally!)
Foodsmart Jonesboro Ar Weekly Ad
14 Top-Rated Attractions & Things to Do in Medford, OR
Walgreens On Bingle And Long Point
Egusd Lunch Menu
Delete Verizon Cloud
Planned re-opening of Interchange welcomed - but questions still remain
Solve 100000div3= | Microsoft Math Solver
Blackstone Launchpad Ucf
The Syracuse Journal-Democrat from Syracuse, Nebraska
Spectrum Outage in Genoa City, Wisconsin
Convenient Care Palmer Ma
What Is Kik and Why Do Teenagers Love It?
Join MileSplit to get access to the latest news, films, and events!
Lbl A-Z
Subdomain Finder
Coroner Photos Timothy Treadwell
Best Conjuration Spell In Skyrim
Timothy Warren Cobb Obituary
Ts In Baton Rouge
3367164101
Server Jobs Near
Ephesians 4 Niv
Gander Mountain Mastercard Login
Dineren en overnachten in Boutique Hotel The Church in Arnhem - Priya Loves Food & Travel
Craiglist.nj
Heat Wave and Summer Temperature Data for Oklahoma City, Oklahoma
Call2Recycle Sites At The Home Depot
Unbiased Thrive Cat Food Review In 2024 - Cats.com
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 6288

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.