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}