April 30, 2020 at 3:59 am #223998ParticipantTopics: 2Replies: 3Points: 25Rank: Member
Hi, I’m stuck on a variable question.
Q:- Create two variables in PowerShell, one with the value of John and one with the value of Miller.
My Ans:- $firstname =”John”
Create a third variable in PowerShell that will contain the two variables you just created, so that it returns John Miller
My Ans:- $fullname =”$firstname $secondname”
Now comes the confusing part.
Q:- If you change the first or second variable, the third variable should automatically change?
But When I changing the value of either first name or second name, the value of full name is not changing automatically.
I am attaching the screenshot. https://prnt.sc/s8dbx2
April 30, 2020 at 6:30 am #224046ParticipantTopics: 9Replies: 707Points: 2,847Rank: Community Hero
No it definitely won’t affect it. They are not references, you are taking the values of $firstname and $secondname and putting them into an unrelated variable.
April 30, 2020 at 6:31 am #224049ParticipantTopics: 6Replies: 20Points: 68Rank: Member
Referencing your codes:$firstname = “John”
$Secondname = “Miller”
$fullname = “$firstname $Secondname” # you assigned/changed the variable
$fullname # output: John Miller
$firstname = “james” # this changed the $firstname variable
$fullname # output: John Miller because the variable $fullname was not changed
</div>to get the desired result, you must update the $fullname variable
$fullname = “$firstname $Secondname”
$fullname # output: james Miller
- The topic ‘Changing Powershell variable value not reflecting during echo’ is closed to new replies.