rajesh
rajesh

Reputation: 3

How to read values of the properties file in the powershell script

I have a properties file properties.txt and I have the following values in it

filename = atc
 extension = zip

path = D:\root
and so on ..

so on I have some properties in the text file which I want to use these values in the .ps1 file

eg when I want to use the path in the .ps1 file I want the .ps1 file to read from the properties.txt file. I am trying above code but it is not working . I wanted to read all the values

$props_file = Get-Content "D:\code"
$props = ConvertFrom-StringData ($properties.txt)

can any one please help me ?

Upvotes: 0

Views: 7158

Answers (1)

J. Allen
J. Allen

Reputation: 592

How about this?

 $fileContents = get-content c:\path\properties.txt

 $properties = @{}

 foreach($line in $fileContents)
 {
     write-host $line
     # ,2 tells split to return two substrings (a single split) per line
     $words = $line.Split('=',2)
     $properties.add($words[0].Trim(), $words[1].Trim())
 }

 $properties

Then you can use the values like this

$properties.Extension
$properties.path

Upvotes: 2

Related Questions