13

I've got a couple of Powershell scripts that automate DLL transferal and I'd like to import the variables from one text file into the various scripts. My example:

Variables.txt
$foo = "blarg"
$bar = "other blarg"

And then I'd like to do something like this:

Script.ps1
Imports Variables.txt
echo "$foo $bar"
0

1 Answer 1

31

This can be accomplished by Dot Sourcing.

Create a .ps1 file, declare your variables in it, then dot source the file. This will bring any variables declared in the file into the global scope.

Example:

Contents of Variables.ps1:

$foo = "blarg"
$bar = "other blarg"

Dot source it:

. ./Variables.ps1
Write-Host "$foo $bar"
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a workaround to use the path for the file that's in a variable? "I'm getting . The term ./$filepath is not recognized as the name of a cmdlet, function, script file, or operable program" Error
Hi @Princy, try putting the variable in double quotes, like this: . .\"$filepath"

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.