Powershell ->read specific section of a file

Tsvetkov, Martin 22 Reputation points
2021-05-28T06:58:37.643+00:00

Hello. I have ini file with different sections like:

[section1]
bla bla 1
bla bla 2

[Section2]
blala 3
bla bla 4

etc.
How can I read in a variable specific section of the file.
p.s. The brackets can be changed , even the structure of the file.
Thank you in advance.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,509 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Chris 656 Reputation points
    2021-05-30T08:51:13.457+00:00

    please take a look of this. maybe it's helpfull

    https://www.dev4sys.com/2016/05/how-to-parse-ini-file-in-powershell.html

    0 comments No comments

  2. Chris 656 Reputation points
    2021-05-30T08:51:34.783+00:00
    0 comments No comments

  3. Tsvetkov, Martin 22 Reputation points
    2021-06-27T13:56:00.36+00:00

    Thank you both. If I make the file look like CSV it would be easy.

    0 comments No comments

  4. Rich Matheisen 46,551 Reputation points
    2021-06-28T02:18:28.83+00:00

    You might be able to use this:

    function ConvertFrom-Ini{
        [CmdletBinding()]
        param (
            [Parameter()]
            [string[]]
            $str
        )
        $ns      = ''
        $sections = [ordered]@{}
        $counter = 0
    
        [array]$lines = $str -split "(?:\015{1,2}\012|\015|\012)"
        ForEach ($line in $lines){
                $counter++;
    
                # Skip comments and empty lines.
                if ($line -match "^\s*(?:\#|\;|$)"){
                    continue
                }
    
                # Remove inline comments.
                $line = $line -replace "\s\;\s.+$",""
    
                # Handle section headers.
                if ( $line -match "^\s*\[.*\]\s*$" ){              # is this a section header?
                    # Create the sub-hash if it doesn't exist.
                    # Without this sections without keys will not
                    # appear at all in the completed struct.
                    if ( $line -match "^\s*\[\s*(.+?)\s*\]\s*$" ){  # does the section have a name?
                        $ns = $matches[1]
                        $sections[$ns] = @()
                        continue
                    }
                    $sections[""] = @()
                    continue
                }
    
                # Handle properties.
                if ( $line -match "^\s*([^=]+?)\s*=\s*(.*?)\s*$" ){
                    $sections[$ns] += @{$matches[1]=$matches[2]}
                    continue
                }
                throw "Syntax error at line $counter : '$_'"
        }
    
        $sections
    }
    

    Here are a few examples of using it:

    # test
    $ini = @"
    rootproperty=blah
    
    [section]
    reg_exp_1=High Priority
    reg_exp_2=Low Priority
    three= four
    Foo =Bar
    empty=
    "@
    
    $x=ConvertFrom-Ini $ini
    # show all section and keys/values
    $x.Keys |
        ForEach-Object{
            "[$_]"                              # section name
            $sec = $_
            $x[$_].keys |                       # all keys in section
                ForEach-Object{
                    "`t$_ = $($x[$sec].$_)"     # key = value
                }
        }
    
    # if you know the section and value name
    $x["section"].Foo
    
    # Export section/keys/values to CSV
    $x.Keys |
        ForEach-Object{
            $sec = $_
            $x[$_].keys |                       # all keys in section
                ForEach-Object{
                    [PSCustomObject]@{
                        Section = $sec
                        Name = $_
                        Value = $x[$sec].$_
                    }
                }
        } | Export-Csv c:\junk\ini.csv -notypeinfo
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.