Read .ini files with Powershell
In order to use the script below create an .ini file that contains one or more sections encapsulated in [] brackets and save it in the same folder as the script. The example uses a file called MyConfig.ini that contains the contents below:
;This is a comment.
[TestSection01]
01=Data from “01”= in section TestSection01
02=Data from “02”= in section TestSection01
03=Data from “03”= in section TestSection01
[TestSection02]
04=Data from “04”= in section TestSection02
05=Data from “05”= in section TestSection02
06=Data from “06”= in section TestSection02
[TestSection03]
07=Data from “07”= in section TestSection03
08=Data from “08”= in section TestSection03
09=Data from “09”= in section TestSection03
Copy the code below and save it with a filename and a .ps1 extenstion.
<pre class="wp-block-syntaxhighlighter-code"><strong>Function GetIniContent {
[CmdletBinding()]
Param(
[ValidateNotNullOrEmpty()]
[ValidateScript({(Test-Path $_) -and ((Get-Item $_).Extension -eq ".ini")})]
[Parameter(ValueFromPipeline=$True,Mandatory=$True)]
[string]$FilePath
)
Begin
{Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"}
Process
{
Write-Verbose "$($MyInvocation.MyCommand.Name):: Processing file: $Filepath"
$ini = @{}
switch -regex -file $FilePath
{
"^\[(.+)\]$" # Section
{
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
}
"^(;.*)$" # Comment
{
if (!($section))
{
$section = "No-Section"
$ini[$section] = @{}
}
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = "Comment" + $CommentCount
$ini[$section][$name] = $value
}
"(.+?)\s*=\s*(.*)" # Key
{
if (!($section))
{
$section = "No-Section"
$ini[$section] = @{}
}
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Processing file: $FilePath"
Return $ini
}
End
{Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"}
}
<# Script process begins here. #>
#Declare array variables that will hold the values in each section.
$TestSection01 = @()
$TestSection02 = @()
$TestSection03 = @()
#Read through the hash table returned by function GetIniContent.
$FileContent = GetIniContent ".\MyConfig.ini"
foreach ($i in $FileContent.Keys)
{
Foreach ($j in ($FileContent[$i].keys | Sort-Object))
{
if ($i -like '*TestSection01*') {
if ($j -match "Comment[\d]+") {
$TestSection01 += "$($FileContent[$i][$j])"
}
else {
$TestSection01 += "$($FileContent[$i][$j])"
}
}
elseif ($i -like '*TestSection02*') {
if ($j -match "Comment[\d]+") {
$TestSection02 += "$($FileContent[$i][$j])"
}
else {
$TestSection02 += "$($FileContent[$i][$j])"
}
}
elseif ($i -like '*TestSection03*') {
if ($j -match "Comment[\d]+") {
$TestSection03 += $j + "=" + "$($FileContent[$i][$j])"
}
else {
$TestSection03 += $j + "=" + "$($FileContent[$i][$j])"
}
}
}
}
#Read through the .ini sections arrays and do something with the info.
foreach($TestSection01s in $TestSection01) {
Write-Host "Do something with $TestSection01s"
}
foreach($TestSection02s in $TestSection02) {
Write-Host "Do something with $TestSection02s"
}
foreach($TestSection03s in $TestSection03) {
Write-Host "Do something with $TestSection03s"
}</strong></pre>
Have some powershell code you need to get done?
Hire us
We’ll sit down with you and go over what your requirements are and get this off of your to-do list.