-
Notifications
You must be signed in to change notification settings - Fork 52
Get IniContent
This function reads the content of a text file and returns a HashTable.
Up to version 1.1.0, the function returned
HashTable
Version 1.1.0 changed to
System.Collections.Specialized.OrderedDictionary
this change is aligned to PowerShell v2. The benefit is keeping the order of the imported keys.
The function accepts multiple file paths. This will return an array of HashTables. This is illustrated in Example 2: Get data to connect to FTP from Example 1.
- The section can be queried with
$iniData.keys
- The content of a section can be queried with
$iniData.SectionName
or$iniData["SectionName"]
- The keys can be queried with
$iniData.SectionName.keys
or$iniData["SectionName"]
- The value of a key can be queried with
$iniData.SectionName.Key
or$iniData["SectionName"]["Key"]
This parameter describes the path of the file to be read. Ideally this would be a path to a file with the INI extension. Type: String
This parameter must be provided. The value cannot be empty or null. If the path points to a non-existent file, an empty ini object will be returned.
The path is tested with Test-Path $_
This parameter can be provided over the pipeline:
$IniContent = Get-ChildItem c:\sample\*.ini | Get-IniContent
Name Value ---- ----- Key1 Foo Key2 Bar Comment1 ;comment line
This parameters specifies what characters characterize a comment line. Type: Array of chars
By default a semicolon is used ;
. This can be extended or overwritten.
Reading a INI file that uses #
as comment character:
Get-IniContent -FilePath "C:\sample\config.ini" -CommentChar @("#")
Name Value ---- ----- key1 Foo Key2 Bar Comment1 #comment line
This parameter keeps the function from returning comment lines. Type: Switch
Get-IniContent -FilePath "C:\sample\config.ini" -IgnoreComments
Name Value ---- ----- Key1 Foo Key2 Bar
By default the Module provides an alias gic
for the function.
This section uses these two INI files:
--FILE 1: ftp_profile.ini--
[settings]
;server address
server = ftp://contoso.com
;Username
user = admin
;Password
password = Password!
;FTP Mode
mode = passive
--FILE 2: transfer.ini--
[upload]
files = index.html,styles.css,script.js
overwrite = true
source = c:\temp\website\
[download]
files = images/loading.gif,images/logo.png
destination = c:\temp\website
Get all data
Get-ChildItem *.ini | Get-IniContent
Name Value ---- ----- settings {Comment1, server, Comment2, user...} upload {files, overwrite, source} download {files, destination}
Get data to connect to FTP from Example 1
$ftpData = Get-ChildItem *.ini | Get-IniContent
($ftpData | ? {$_["settings"]})["settings"]
Name Value ---- ----- Comment1 ;server address server ftp://contoso.com Comment2 ;Username user admin Comment3 ;Password password Password! Comment4 ;FTP Mode mode passive
Get data to connect to FTP
$ftpData = Get-IniContent -FilePath "ftp_profile.ini"
$ftpData["settings"]
Name Value ---- ----- Comment1 ;server address server ftp://contoso.com Comment2 ;Username user admin Comment3 ;Password password Password! Comment4 ;FTP Mode mode passive
Get data to connect to FTP ignoring comments
$ftpData = Get-IniContent -FilePath "ftp_profile.ini" -IgnoreComments
$ftpData["settings"]
Name Value ---- ----- server ftp://contoso.com user admin password Password! mode passive
Loop through all file to be uploaded
$ftpTransfer = Get-IniContent -FilePath transfer.ini
$ftpTransfer["upload"]["files"].Split(",") | % {Write-Host "Upload: $_"}
Upload: index.html Upload: styles.css Upload: script.js