Import variables from a text file into Powershell

I have several Powershell scripts that automate the transfer of DLLs, and I would like to import variables from one text file into different scripts. My example:

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

And then I would like to do something like this:

 Script.ps1 Imports Variables.txt echo "$foo $bar" 
+4
source share
1 answer

This can be done with Dot Sourcing .

Create a .ps1 file, declare your variables in it, then specify the source of the source. This will transfer any variables declared in the file to the global scope.

Example:

Contents of Variables.ps1:

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

Point Source:

 . ./Variables.ps1 Write-Host "$foo $bar" 
+9
source

Source: https://habr.com/ru/post/1441338/


All Articles