William Ross
William Ross

Reputation: 3910

How to read variables in Powershell from a GitHub text file

I have a text file in GitHub at a location like: https://github.com/raw/my-account/my-project/paths.txt

The text file contains key-value pairs separated by an '=' sign like:

key1=value1
key2=value2
key3=value3

I want to gather these variables into my Powershell script. If the text file is local I can do it with:

Get-Content paths.txt | Foreach-Object{
   $var = $_.Split('=')
   New-Variable -Name $var[0] -Value $var[1]
}

To read the text file from GitHub I tried replacing 'paths.txt' with the GitHub location of the text file surrounded by double-quotes. However, it says a drive letter 'https' cannot be found.

How can I read the text file from GitHub and store the variables into my Powershell script?

Upvotes: 0

Views: 1138

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174545

Use ConvertFrom-StringData (Version 3.0+):

$webRequest = Invoke-WebRequest https://github.com/raw/my-account/my-project/paths.txt
$paths = ConvertFrom-StringData -StringData $webRequest.Content

$paths is now a hashtable with a key per unique name in the input data:

PS C:\> $paths['key1']
value1

Upvotes: 3

Related Questions