Script with fields - variable

Hi,
I made a script in PowerShell and I need your help.
####################################################
clear
$A1=“line1”
$A2=“line2”
$A3=“line3”

$fields= “A1”,“A2”,“A3”
ForEach (ligne in ($fields))
{
$ligne+";"
}
####################################################
When I execute the script, it display
A1;
A2;
A3;
But I want the result of the fields like that :
line1;
line2;
line3;

hello @algilber

Just directly pass the variables as $A1, $A2, $A3 to the $fields

clear
$A1=“line1”
$A2=“line2”
$A3=“line3”
    
$fields= $A1,$A2,$A3
 // or $fields = "line1", "line2", "line3"
ForEach ($ligne in ($fields))
{
$ligne+";"


}

Hope it helps

1 Like

Alain,
welcome to the forums.

While Rahul already showed you how to achieve what you wanted it looks like you’re trying to solve a bigger issue you’re not showing here yet. There might be a better way to achieve what you actually want to do in the end?! :wink:

Regardless of that: Could you please format your code as code here in the forum?
Thanks in advance.

2 Likes

Hello, @algilber.

This is an alternative to @Rahul20’s answer, which produces the same output; but it demonstrates the language flexibility in PowerShell. Like @Olaf, I am curious about the larger picture. Knowing the intentions for your program helps to assess the route to an accurate and efficient solution. If you are new to PowerShell, this forum can also recommend educational resources and practices for quicker learning.

Demo code

cls
$A1=“line1”;$A2=“line2”;$A3=“line3”;
($A1,$A2,$A3).ForEach({"$_;"})

Results_01

Sincerely,
-d