How to access a variable in one script in another script

How can I access a variable in Script1 in a different script: Script2?

I think you accidentally typed this into instead of bing/google. However, in case i’m mistaken, check out this link.

I just want to access the variable in script 1 in Script2, I do not want to run the whole script i.e. Script1 from Script2

That’s impossible. A variable starts to exist when its assignment has run. So either you include the other script or you parse it by yourself and pick out what you need or you outsource the variables and setting you need in both scripts to a third script you could dot source in both scripts.

Thanks. What about making it is a module and exporting the variable. I am getting an error saying the The Export-ModuleMember can only be called from inside a module whereas I am inside a module only.

You may describe what you’re ACTUALLY trying to accomplish - not the attempted solution. There might be a better way. :wink:

You may also want to (re-)read the help for about_Scopes

I want to access the output file stored a in variable in one script as input of of another script. Say a text file.

Hmmm … that’s still quite vague. Did you place the variable there or is it dynamic or where does it come from? You still can save it in a settings file you access from both scripts. Or you should elaborate much more to make us understand. :wink:

to save the content of a variable to a file check out this link
to read content from a file check out this link


I’d think Sam speaks about a file path assigned to a variable in another script file - not the content of the file itself. :wink:

I created a variable with a path of a text file in Script1. In Script1, I am using Out-File to store the output of this script to this file.


$finaloutput | Out-File $out

I want to use this $out variable as input of another script. Since the $out will change each time Script1 is run, I want to make sure , I use the correct file as input in the other script.

You could use an external place to store this file path. Either a ragistry key or a settings file in plain text or CSV or XML or JSON. You could output the file path as the result of the script execution of Script1 and use this as the parameter to call the script 2 from script 1 or - as I already mentioned above - you parse script 1 from script 2.

Who is changing script 1 actually?

Since the script runs on a regular basis say twice in a week. Each time the Script1 runs, it will create a new file with different name and this same file needs to be used as input for the other script.

OK, so it’s dynamic. I actually already asked this. :-/ When you know the algorythm script1 uses to create the file name you can use the same in script 2. If the file is always created in the same folder you could simply always use the most recent file in that folder. OR … and that will not change … you write the file name script1 creates in a place accessible for script2. There’s no other way.

Thanks, I am aware of this approach. Thought may be there is a better way. :slight_smile:

the name of the file changes each time you run it. if I understand you correctly if you run the file today the filename would be out_29-04-20.txt

#check if the file exists

$date = get-date -format “dd-MM-yy”
$filename = “out_”+ $date +“.txt”
$fileToCheck = “C:\temp$filename”
if (Test-Path $fileToCheck -PathType leaf) {
write-host “file exists”
} ELse {
write-host “file not found” -ErrorAction Stop

#second script
$out = get-content $fileToCheck