Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Published on
3,555 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.

Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Join & Write a Comment

In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
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…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month