1

I try to make a program that gets variables from an external file. My problem is that company policy doesn't allow users to run ps1 files (the program works because I compile the ps1 into an exe by ps2exe), so this external file must be a txt, csv, or something other plain text format. I don't want to burn these variables into the exe, because the users need some flexibility with the values they use.

I tried to make a hashtable and get the variables into it by ConvertFrom-StringData, but that messes up the output.

file.txt

one = foo
two = bar

prog.ps1

$variable = Get-Content ".\file.txt" | Out-String | ConvertFrom-StringData
Write-Host $variable.one
Write-Host "$variable.two"

The latter one shows up as: System.Collections.Hashtable.two instead of the value of $variable.two. Of course, in this simplified example there wasn't really need for the quotation marks around the second variable, but this program needs it at some places.

When I stored the variables with their correct format in a ps1 file, and inculded it the normal

. .\file.ps1

way, it worked like it should, but it can't be used by the users who need it.

I would appreciate any ideas of how to make it work. I mean if it's possible at all.

2 Answers 2

2

What you are doing is working, but you're using the wrong syntax for quoted variables.

It should be:

$variable = Get-Content ".\file.txt" | Out-String | ConvertFrom-StringData
Write-Host $variable.one
Write-Host "$($variable.two)"

For more information, see this answer.

1

Your problem is merely the incorrect use of interpolation syntax inside an expandable string ("..."):

"$variable.two" expands $variable by itself, followed by literal .two, because in order to embed expressions in expandable strings you must enclose them in $(...), i.e., in this case, "$($variable.two)" - see this answer for a summary of PowerShell's string-expansion rules.

Perhaps surprisingly, Write-Host $variable.one - without enclosing "..." - successfully accessed the property value, because it is a value expression rather than an (implicitly) expandable string; this answer has more information about how unquoted tokens are parsed as command arguments.

Also:

  • If you're running in PowerShell v3 or higher, Get-Content -Raw is simpler and more efficient than ... | Out-String to get a text file's entire content as a single, multi-line string.

  • As an aside: Write-Host is typically the wrong tool to use, unless the intent is to write to the display only, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, redirect it to a file. In PSv5+ Write-Host writes to the information stream, whose output can be captured, but only via 6>. See the bottom section of this answer for more information.

To put it all together:

$variable = Get-Content -Raw .\file.txt | ConvertFrom-StringData

'--- implicit output, to success stream'
$variable.one
$variable.two

'--- explicit to-host output, via expandable string'
write-host $variable.one
write-host "$($variable.two)"  # Note the $(...) around the property access

The above yields:

--- implicit output, to success stream
foo
bar
--- explicit to-host output, via expandable string
foo
bar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.