Go Premium for a chance to win a PS4. Enter to Win


[Powershell Tips] Convert String Data into a hash table (ConvertFrom-StringData)

Published on
3,794 Points
2 Endorsements
Last Modified:
Sunil Chauhan
Expertise in Microsoft Exchange Server, Office 365, and PowerShell scripting, In my spare time I love writing tech blogs and taking photos.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.

If you have a PowerShell script running/scheduled in your environment, there are settings that may require editing from time to time, most commonly SMTP settings.  If you prefer not to have people edit these settings and mess around with the code within the Script, then you can utilize the "ConvertFrom-StringData" cmd which converts string data into a hash table.

For those who do not know what hash table is,

A hash table, also known as a dictionary or associative array, is a compact data structure that stores one or more key\/value pairs. For example, a hash table might contain a series of IP addresses and computer names, where the IP addresses are the keys and the computer names are the values, or vice versa.

An example method of how a hash table can be created is shown below.



Example: Student Information saved in a Hash Table

$student=@{"Class"="B.com";"Name"="Sunil Chauhan";}

Further, we can fetch specific details from the hash table by calling the Name key.

$Student.Name would return "Sunil Chauhan" for example.

Now let's say we are building a script below where SMTP settings will be required to be changed based on the environment.  We have saved these settings in a text file parameter and value delimited with "=".  As all the settings that may require modification are stored in a "Settings.txt" file, it will be a simple matter to edit any required settings.


Next, if we fetch the contents of this text file in a PowerShell array, we will have data in string format as shown in the picture below.

As this data is currently in string format, fetching any particular details from the file would require us to do more complex string manipulation, editing, and splitting etc. 

To make things simple, convert the string data that was saved in the "Settings.txt" file into a hash table.

So to convert the string data saved in $TextData array as shown in the previous step (Picture above) use the ConvertFrom-StringData cmd.

$settings = $TextData | ConvertFrom-StringData

Now that StringData has been successfully converted to a hash table, this can further be used by simply calling the Key Name to get the required values.

For instance, $Settings.to will return the Email address stored in "TO" field and $Settings.SmtpServer will return the SMTP server value and so on.

Example: Sending emails using the settings from Hash Table.

Send-MailMessage -To $settings.To -From $settings.from -SmtpServer $settings.SmtpServer -Port $settings.Port

In this post I have demonstrated how settings can be saved in a text file to manage them easily and then converting the StringData from the text file into a hash table. I hope this post was informative, please feel free to leave your feedback.

For more details on ConvertFrom-StringData please visit MSDN post here, follow me on LinkedIn or at my blog @ www.sunilchauhan.info for more such cool tips.


Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Join & Write a Comment

If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month