0

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 ?

Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
rajesh
  • 3
  • 1
  • 5
  • 1
    Define "not working." What *exact* behavior are you experiencing? What behavior are you desiring? – Bacon Bits Aug 07 '16 at 04:31
  • Note that in a properties file a single '\' is an escape character, so you'll need double '\\' to have it be valid for Java and some other languages. – dragon788 Oct 17 '16 at 16:55

1 Answers1

2

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
dragon788
  • 3,583
  • 1
  • 40
  • 49
J. Allen
  • 602
  • 1
  • 7
  • 24
  • 1
    The challenge with using a Split is that there could be multiple '=' in one line, say for a nested URL that has a query string with additional `value=someval` statements. The syntax you gave for querying the values should work with the way he's parsed the file using `ConvertFrom-StringData`. – dragon788 Oct 17 '16 at 15:53
  • @dragon788 I approved your edit to include adding an argument to $line.Split(). – J. Allen Oct 17 '16 at 17:31