I’d like to create a new variable and have the value “one, two, three” splited with a line break and added some text. The end result “MyValue2” should look as follows:
I’m a newbe in PS and would appreciate some hints how to achieve this. Only “one”, “two” and “three” is pulled from the first variable the rest is additional fix text. Thank you for pointing me in the right direction.
Not really sure what you are trying to accomplish, but you are working with a hash table.
$MyVariable['MyValue'] = "one, two, three"
Keep in mind that this is a string:
“one, two, three”
and this is an array:
“one”, “two”, “three”
These are parsed differently, so you need to understand what you are working with. With what you posted, a string, you would need to use -split to make the string into an array to loop through it and since you have a space after the comma we need to trim it to get “one” versus "one ".
Thanks you so much for your code. It helped me a bit but still one thing I’m not sure how to manage. It seems you have two loops but since this is only part of the variables this will mess things up. Here the adapted code which is working for me:
$MyVariable = @{}
$MyVariable.MyValue = "one, two, three"
$MyVariable.OtherValue = "whatever"
$MyVariable.Etc = "and so on"
$MyVariable.MyValue2 = foreach ($val in ($MyVariable.MyValue -split ',')) {
'My fix text "{0}&' -f $val.Trim() +"`""
}
$MyVariable
Name Value
---- -----
MyValue2 {My fix text "one&", My fix text "two&", My fix text "three&"}
OtherValue whatever
MyValue one, two, three
Etc and so on
$MyVariable.MyValue2
My fix text "one&"
My fix text "two&"
My fix text "three&"
That is what I tried to achieve, but there is one “fix text line” at the top of $MyVariable.MyValue2.