0

I have a powershell file in which i have a variable named $CurrentReleaseNumber :

$CurrentReleaseNumber="4.5"

What i want to do is edit the value of this variable inside another powershell file. This second powershell file will update the value of this variable and the new value should now reflect in first powershell file. So after execution of second powershell file the first powershell file should look like :

$CurrentReleaseNumber="4.7"

5
  • 2
    Could you provide example of what you want to achieve? Commented Nov 17, 2015 at 10:53
  • Hi , I have a powershell file having a variable named CurrentReleaseNumber. What I want to do is access this variable inside another powershell file and edit its value. The new value I will receive at run time while running the second powershell file. Commented Nov 17, 2015 at 11:06
  • 1
    Sorry, I still does not understand your goal. Do you want to edit script file, or you doing something else? Please provide expanded sample of what you have, what you have tried, and how current results are different from desired results. And, please, does not do that in comments, edit you question instead. Commented Nov 17, 2015 at 12:21
  • Programming is a fickle thing. Use slightly different terminology and you get entirely different results. Please keep this in mind when asking questions. For example, will you receive these new values before running the second script? Will the first script launch the second? If in doubt, post everything. Commented Nov 17, 2015 at 14:08
  • question edited.Please reply. Commented Nov 17, 2015 at 17:26

1 Answer 1

1

You can try this, it is called dot-sourcing (assuming both files are in the same folder and you are using PS version 3 or higher) :

script1.ps1 :

$myVariable = "hey !"

script2.ps1 :

. "$PSScriptRoot\script1.ps1"

$myVariable

Output :

hey !
Sign up to request clarification or add additional context in comments.

4 Comments

Hi if i change myvariable in script2 will it reflect in ps1 also. Please reply
@tatushar3 no, it won't, it'll copy them into the current scope
As already said by @PetSerAl, Please provide expanded sample of what you have, what you have tried, and how current results are different from desired results. And, please, does not do that in comments, edit you question instead. Then we will be able to help you further
Your new requirement implies to actually modify the script.ps1 file which is quite different. I don't have a solution for this, and it seems really weird to me : ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.