Get an INI file value into a variable

Hi, I’m new to powershell and working with some courses, but now i need to figure out how i can get an INI file value into a variable i’m going to use later in the script.

My Ini file looks like this:

[Install308046B0AF4A39CB]
Default=Profiles/eyk4sjht.default-release
Locked=1

How can i get this value out: eyk4sjht.default-release into a variable called $INIvalue

So far i only got this:

$INI = Get-Content "C:\Temp\Scripts\profiles.ini"
$INI

Assuming your INI file really looks like you posted it the easiest way to make it available for a PowerShell script is to skip the first line and convert the rest of the file with ConvertFrom-StringData.

$INI = 
    Get-Content -Path 'C:\Temp\Scripts\profiles.ini' | 
        Select-Object -Skip 1 | 
            ConvertFrom-StringData

Now you can access the individual settings with the dot notation like this:

$INI.Default
$INI.Locked
1 Like

Thanks Olaf, I’m on the right way here.
But, i just pasted in the part of the INI file that i need the information into a varaible. There are more information in this file, as you can see below

This is the top 2 brackets of the ini file, but there are several of them, and contain even same values e.g Default as shown here:

[Install308046B0AF4A39CB]
Default=Profiles/eyk4sjht.default-release
Locked=1

[Profile1]
Name=default
IsRelative=1
Path=Profiles/zqjnpnuu.default
Default=1

And, i see from you contribution that $Ini.Default now gives me “Profiles/eyk4sjht.default-release” where the Profiles is the folder… i just need “eyk4sjht.default-release”

It would have been nice if you provided all needed inforamtion. :smirk: And it would be easier to help you if you asked specific questions instead of stating facts about the status quo or just telling what you’re after. :smirk: And you are allowed to use the code suggestions you get here and extend and adapt them to your particular needs.

Please always read the help for the cmdlets you’re about to use completely including the examples to learn how to use them AND to learn what else they are capable of. :wink:

Additionally this might help …

I’m Sorry.
I thought this was reasonably easy, and after going through the Powershell 7 Essential Training, I’ve fiddled a bit more.
Probably not the neatest script, but I can put the correct value into the variable.

$FileLocation = 'C:\temp\Scripts\profiles.ini'
$AppLine = Get-Content -Path $FileLocation | Where-Object { $_ -match 'Default=Profiles/' }
$INITempValue = $AppLine -split '/', 2 
$IniValue = $INITempValue | Select-Object -skip 1
$INIValue

There’s no need to … everything’s fine. :+1:t4: :slightly_smiling_face:

It actually is. :man_shrugging:t4: :slightly_smiling_face:

I’m actually proud of you. :+1:t4: :grinning: You just opened the door to a whole new world. :wink:

That doesn’t matter. All that counts is the result.

Thanks for sharing your final code. That might help others looking for a solution for the same or a similar issue.

2 Likes

Here’s a little snippet that can turn your ini into objects with two properties, Section (the ini section header) and Properties (the properties of that section)

First, I am going to make a test file so we can all run this test.

$file = New-TemporaryFile

@"
[Install308046B0AF4A39CB]
Default=Profiles/eyk4sjht.default-release
Locked=1

[Profile1]
Name=default
IsRelative=1
Path=Profiles/zqjnpnuu.default
Default=1
"@ | Set-Content $file.fullname

Ok, now that we have our ini file, here is the code to pull the info out

$content = Get-Content $file.FullName -Raw

$output = $content -split '(?=\[.+?])' | ForEach-Object {
    $ht = [ordered]@{}

    switch -Regex ($_ -split '\r?\n'){
        '\[(?<Section>.+?)]' {
            $ht.Section = $Matches.section
            $ht.Properties = [ordered]@{}
        }

        '=' {$ht.Properties += $_ | ConvertFrom-StringData}
    }

    if($ht.keys.count -gt 0){
        [PSCustomObject]$ht
    }
}

For this example, $output should have two objects. You can inspect them in various ways. Since it’s a multidimensional object, ConvertTo-Json can format it nicely for us to view

$output | ConvertTo-Json

[
    {
        "Section":  "Install308046B0AF4A39CB",
        "Properties":  {
                           "Default":  "Profiles/eyk4sjht.default-release",
                           "Locked":  "1"
                       }
    },
    {
        "Section":  "Profile1",
        "Properties":  {
                           "Name":  "default",
                           "IsRelative":  "1",
                           "Path":  "Profiles/zqjnpnuu.default",
                           "Default":  "1"
                       }
    }
]

You can pick out the section you need.

$output | Where-Object Section -like *install*

Section                 Properties       
-------                 ----------       
Install308046B0AF4A39CB {Default, Locked}

If you save it to a variable you can further inspect/extract.

$desiredsection = $output | Where-Object Section -like *install*

$desiredsection.Properties

Name                           Value                                                                                                                                                          
----                           -----                                                                                                                                                          
Default                        Profiles/eyk4sjht.default-release                                                                                                                              
Locked                         1 

And finally if you want just the portion of Default that comes after the slash, you have a few options. Here are a couple.

# store the property value in a variable
$desiredproperty = $desiredsection.Properties.Default

# each of these should output the desired portion "eyk4sjht.default-release"
$desiredproperty -replace '^.+/'

$desiredproperty.Split('/')[1]

$desiredproperty -split '/' | Select-Object -Last 1

[regex]::Match($desiredproperty,'(?<=/).+?$').value

if($desiredproperty -match '(?<=/).+?$'){$Matches.0}

You can also use the Get-PrivateProfileString function in my module PoshFunctions as found on the Powershell Gallery