Changing Powershell variable value not reflecting during echo

Welcome Forums General PowerShell Q&A Changing Powershell variable value not reflecting during echo

  • This topic has 2 replies, 3 voices, and was last updated 1 month ago by
    Participant
    .
Viewing 2 reply threads
  • Author
    Posts
    • #223998
      Participant
      Topics: 2
      Replies: 3
      Points: 25
      Rank: 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”

      $secondname =”Miller”

      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

    • #224046
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: 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.

    • #224049
      Participant
      Topics: 6
      Replies: 16
      Points: 47
      Rank: Member

      Hi,

      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>
      <div>`to get the desired result, you must update the $fullname variable
      $fullname = “$firstname $Secondname”
      $fullname # output: james Miller

       

      • This reply was modified 1 month ago by Gnart.
      • This reply was modified 1 month ago by Gnart.
Viewing 2 reply threads
  • You must be logged in to reply to this topic.