title: Assigning proper variable types when read from a config file [PowerShell]
body: <p>I am trying to read values from a text file and keep them as variables to use in my script.</p>
  
  <p>This config file contains strings, ints, booleans and an array that can contain strings, ints and booleans.</p>
  
  <p>When I declare the variables outright, I have no problems.  My script functions as expected.  However when I am reading in the config file and trying to create variables based on that, I only get the variables declared as strings.  </p>
  
  <p>This creates my config file in the format I would like.</p>
  
  <pre><code>Function Create-Config() {
      If (!(Test-Path config.txt)) {
          $currentlocation=Get-Location
          $parentfolder=(get-item $currentlocation).parent.FullName
          New-Item config.txt -ItemType "file"
          Add-Content config.txt "SERVER_NAME=MyServer"
          Add-Content config.txt "SERVER_LOCATION=$currentlocation"
          Add-Content config.txt "BACKUP_LOCATION=$parentfolder\backup"
          Add-Content config.txt "CRAFTBUKKIT=craftbukkit.jar"
          Add-Content config.txt "JAVA_FLAGS=-Xmx1G"
          Add-Content config.txt "CRAFTBUKKIT_OPTIONS=-o True -p 1337"
          Add-Content config.txt "TEST_DEPENDENCIES=True"
          Add-Content config.txt "DELETE_LOG=True"
          Add-Content config.txt "TAKE_BACKUP=True"
          Add-Content config.txt "RESTART_PAUSE=5"
     }
  </code></pre>
  
  <p>}</p>
  
  <p>However, either I need to change how I create my config file, or change how I import those variables.  I want the config file to be as simple as possible.  I am using this code to import the values:</p>
  
  <pre><code>Function Load-Variables() {
      Get-Content config.txt | Foreach-Object {
          $var = $_.Split('=')
          New-Variable -Name $var[0] -Scope Script -Value $var[1]
      }
  }
  </code></pre>
  
  <p>As you can see, I don't explicitly set the variable, since the variables from the config are different types (booleans, int, array, strings).  However, PowerShell imports these all as strings.  I can import all variables individually (which I may have to do) but I'm still feeling like I will be stuck on the array.</p>
  
  <p>If I declare the array using this command:</p>
  
  <pre><code>New-Variable -Name CRAFTBUKKIT_OPTIONS -Option Constant -Value ([array]@('-o',$true,'-p',25565))
  </code></pre>
  
  <p>I get exactly what I want, but I need to import it from the config file instead of declaring the variable in my script.  The java program is a bit finicky, so I cannot just import that value as a string, or it will not get passed properly and those options get ignored.  I've found the only way it works is to have it as an array (as defined above).  I also want to note that there could be many more config file options presented than in my example.</p>
  
  <p>I am not sure what is the better approach - importing the variables to be declared correctly (what I would like to do), or assuming they cannot be imported as anything other than a string and then parsing that string into the proper variable types after.</p>
  
  <p>I have tried declaring the variables before hand and using the Set-Variable command to set the values, but that doesn't work. It very much seems like my variables are being imported with Get-Content as strings from the start instead of the correct types. </p>
  
  <p>Full script is here:
  <a href="https://gist.github.com/TnTBass/4692f2a00fade7887ce4" rel="nofollow">https://gist.github.com/TnTBass/4692f2a00fade7887ce4</a></p>