How to append strings to other strings in a data set?

append, powershell, string

Solution

There are many ways. Here are a few:

# Using string concatenation
'Test1','Test2','Test3' | Foreach-Object{ $_ + '.com' }

# Using string expansion
'Test1','Test2','Test3' | Foreach-Object{ "$_.com" }

# Using string format
'Test1','Test2','Test3' | Foreach-Object{ "{0}{1}" -f $_,'.com' }

Problem

I want to append several strings in a data set with custom strings. Example Content of Dataset: ``` Test1 Test2 Test3 ``` Result after appending: ``` Test1.com Test2.com Test3.com ``` Would I have to use regex to parse to the end of each Test[n] to be able to append it with a custom string (`.com`)? Has anyone got an example that describes exactly how to do it? I am reading from a SQL-Table and writing values into a DataSet which is exported to CSV the following way: ``` $DataSet.Tables[0] | ConvertTO-Csv -Delimiter ',' -NotypeInformation |`% { $_ -replace '"','' } | out-file $outfile -Encoding "unicode" ``` The DataSet contains of Strings such as: ``` Banana01 Banana02 Apple01 Cherry01 Cherry02 Cherry03 ``` The thing I want to do is append `.com` to only `Cherry01`, `Cherry02`, and `Cherry03`, and after appending `.com`, export it as a CSV file.

Original source