CleanCode logo
sitemap
SEARCH:

CleanCode PowerShell Libraries v1.2.08 API: CleanCode » FileTools » Get-IniFile

Get-IniFile

NAME

Get-IniFile

SYNOPSIS

Gets a Windows INI file as a hash table.

SYNTAX

Get-IniFile [[-fileName] <String>] [[-iniHash] <Hashtable>] [<CommonParameters>]

DESCRIPTION

Reads a standard INI file into a hash table allowing direct addressing of each section and each dictionary entry (a key,value pair) within each section. The standard INI format requires all entries to be within a section, but for added flexibility, Get-IniFile allows entries to appear before the first section in which case they are entered into an unnamed pseudo-section.

Only entries having the format "key=value" will be inserted into the hash. Note that it is perfectly legal for a value to itself contain an equals sign; the first equals sign on the line separates the key from the value.

Trailing spaces on any value line are dropped. An octothorp at the beginning of a line comments out the line.

PARAMETERS

-fileName <String>
        Name of INI-formatted file.

        Required?                    false
        Position?                    1
        Default value                
        Accept pipeline input?       false
        Accept wildcard characters?  false

-iniHash <Hashtable>
        A previously built hash table of INI settings. This is an optional parameter needed
        only if you wish to build a hash table from multiple INI files.
        Just pass the result of one call to Get-IniFile back in to the next call.

        Required?                    false
        Position?                    2
        Default value                @{}
        Accept pipeline input?       false
        Accept wildcard characters?  false

<CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, see 
        about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). 

INPUTS

None. You cannot pipe objects to Get-IniFile.

OUTPUTS

System.Collections.Generic.Dictionary[string,string]

NOTES



        Adapted from
        http://stackoverflow.com/questions/417798/ini-file-parsing-in-powershell.

        This function is part of the CleanCode toolbox
        from http://cleancode.sourceforge.net/.

        Since CleanCode 1.1.01.

EXAMPLES


-------------------------- EXAMPLE 1 --------------------------

PS>$myHash = Get-IniFile $myConfigFile; $myHash["auto"]["color"]

This example reads an INI file into a hash table then accesses a single value (the "color" key under the "auto" section).

-------------------------- EXAMPLE 2 --------------------------

PS>$myHash = Get-IniFile $myConfigFile; $myHash.GetEnumerator() | % { "section = $_.name " }; $myHash["auto"].GetEnumerator() | % { "auto[{0}] = {1}" -f $_.name, $_.value }

This example reads an INI file into a hash table, enumerates the section names, then presuming there is a section named "auto", enumerates its keys and values.

RELATED LINKS

-none-

This documentation set was created with CleanCode's DocTreeGenerator.

Valid XHTML 1.0!Valid CSS!Get CleanCode at SourceForge.net. Fast, secure and Free Open Source software downloads
Copyright © 2001-2015 Michael Sorens • Contact usPrivacy Policy
Usage governed by Mozilla Public License 1.1 and CleanCode Courtesy License
CleanCode -- The Website for Clean DesignRevised 2015.12.16