Skip to content
seanjseymour edited this page Jan 3, 2017 · 1 revision

Table of Contents

Description

This function comments out specified keys in all sections or certain sections. The ini source can be specified by a file or piped in by the result of Get-IniContent. The modified content is returned as a ordered dictionary hashtable and can be piped to a file with Out-IniFile.

Usage

Parameters

FilePath

This parameter specifies the path to the input file. Type: String This parameter must be provided if an InputObject is not piped in. The value cannot be empty or null. Parameter position is 0.

InputObject

This parameter specifies the object to be written to the file. Enter a variable that contains the objects or type a command or expression that gets the objects. Type: System.Collections.IDictionary

This parameter must be provided if FilePath is not used. The value cannot be empty or null and can be provided over the pipeline.

CommentChar

Specify what character should be used to comment out entries. Default is a semicolon ";". This parameter is a char array to maintain compatibility with the other functions. However, only the first character is used to comment out entries. Type: Char Array

Keys

String array of one or more keys to limit the changes to, separated by a comma. Optional. Type: String Array

Sections

This parameter specifies a string array of one or more sections to limit the changes to, separated by a comma.

Surrounding section names with square brackets is not necessary but is supported.

Ini keys that do not have a defined section can be modified by specifying '_' (underscore) for the section. Type: String Array

Alias

By default the Module provides an alias aic for the function.

Examples

Example 1

Sample INI file before changes:

[Printers]
Port=3023
Header=Welcome to Test Company Store #<StoreNum>
Footer=(c)2017 My Test Company Inc
Type=USB

Read in the INI file C:\myinifile.ini to $ini, then comment out any keys named 'Header' or 'Footer' in the [Printers] section.

$ini = Add-IniComment -FilePath "C:\myinifile.ini" -Sections 'Printers' -Keys 'Header','Footers'

Sample INI file after changes:

[Printers]
Port=3023
;Header=Welcome to Test Company Store #<StoreNum>
;Footer=(c)2017 My Test Company Inc
Type=USB

Example 2

Sample INI file before changes:

[Terminals]
Front=Reg1
DriveThru=Reg2
Updated=1/1/2017
[Monitors]
Kitchen=1
Fryer=2
Alternate=3
Updated=11/25/2016

Read in the INI file C:\myinifile.ini and comment out any keys named 'Updated' in the [Terminals] and [Monitors] sections. The ini is then piped to Out-IniFile to overwrite the existing INI file at C:\myinifile.ini.

Add-IniComment -FilePath "C:\myinifile.ini" -Sections 'Terminals','Monitors' -Keys 'Updated' | Out-IniFile "C:\myinifile.ini" -Force

Sample INI file after changes:

[Terminals]
Front=Reg1
DriveThru=Reg2
;Updated=1/1/2017
[Monitors]
Kitchen=1
Fryer=2
Alternate=3
;Updated=11/25/2016

Example 3

Sample INI file before changes:

[Printers]
Port=3023
Header=Welcome to Test Company Store #<StoreNum>
Footer=(c)2017 My Test Company Inc
Type=USB
[PrintersOld]
Port=6044
Header=Welcome to Test Company
Footer=(c)2014 My Test Company Inc
Type=Parallel

Read in the INI file C:\myinifile.ini using Get-IniContent, then pipe it to Add-IniComment to comment out any 'Header' keys in any section. The ini is then piped to Out-IniFile to overwrite the existing INI file at C:\myinifile.ini.

Get-IniContent "C:\myinifile.ini" | Add-IniComment -Keys 'Header' | Out-IniFile "C:\myinifile.ini" -Force

Sample INI file after changes:

[Printers]
Port=3023
;Header=Welcome to Test Company Store #<StoreNum>
Footer=(c)2017 My Test Company Inc
Type=USB
[PrintersOld]
Port=6044
;Header=Welcome to Test Company
Footer=(c)2014 My Test Company Inc
Type=Parallel

Example 4

Sample INI file before changes:

Updated=6/23/2009
[Terminals]
Front=Reg1
DriveThru=Reg2
Updated=1/1/2017
[Monitors]
Kitchen=1
Fryer=2
Alternate=3
Updated=11/25/2016

Read in the INI file C:\myinifile.ini using Get-IniContent, then pipe it to Add-IniComment to comment out any 'Updated' keys that are orphaned, i.e. not specifically in a section. The ini is then piped to Out-IniFile to overwrite the existing INI file at C:\myinifile.ini.

Get-IniContent "C:\myinifile.ini" | Add-IniComment -Keys 'Updated' -Sections '_' | Out-IniFile "C:\myinifile.ini" -Force

Sample INI file after changes:

;Updated=6/23/2009
[Terminals]
Front=Reg1
DriveThru=Reg2
Updated=1/1/2017
[Monitors]
Kitchen=1
Fryer=2
Alternate=3
Updated=11/25/2016