Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Variables from Text File in Powershell

Tags:

powershell

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"
like image 361
codo-sapien Avatar asked Oct 22 '12 16:10

codo-sapien


1 Answers

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"
like image 84
dugas Avatar answered Oct 05 '22 23:10

dugas