Search code examples
powershellproperties-file

Read a properties file in powershell


Let's suppose that I have a file.properties and its content is:

app.name=Test App
app.version=1.2
...

how can I get the value of app.name?


Solution

  • You can use ConvertFrom-StringData to convert Key=Value pairs to a hash table:

    $filedata = @'
    app.name=Test App
    app.version=1.2
    '@
    
    $filedata | set-content appdata.txt
    
    $AppProps = convertfrom-stringdata (get-content ./appdata.txt -raw)
    $AppProps
    
    Name                           Value                                                                 
    ----                           -----                                                                 
    app.version                    1.2                                                                   
    app.name                       Test App                                                              
    
    $AppProps.'app.version'
    
     1.2