-
Notifications
You must be signed in to change notification settings - Fork 52
Remove IniComment
This function uncomments 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.
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.
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.
Specify what characters should be recognized as comments when they are present at the beginning of a line. Default is a semicolon ";". Type: Char Array
String array of one or more keys to limit the changes to, separated by a comma. Optional. Type: String Array
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
By default the Module provides an alias ric
for the function.
Sample INI file:
[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, uncomment out any keys named 'Header' in the [Printers] section, and saves the modified ini to $ini.
$ini = Remove-IniComment -FilePath "C:\myinifile.ini" -Sections 'Printers' -Keys 'Header'
Sample INI content of $ini["Printers"] after changes. Note that this has not yet been saved to a file.
> Name Value
> ---- -----
> Port 3023
> Header Welcome to Test Company Store #<StoreNum>
> Footer (c)2017 My Test Company Inc
> Type USB
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 uncomment 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.
Remove-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
Sample INI file before changes:
[Printers]
Port=3023
;Header=Welcome to Test Company Store #<StoreNum>
Footer=(c)2017 My Test Company Inc
Type=USB
[Scanners]
Port=1585
;Header=Store #<StoreNum>
Footer=(c)2017 My Test Company Inc
Type=Serial
Read in the INI file C:\myinifile.ini using Get-IniContent, which is then piped to Remove-IniComment to uncomment 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" | Remove-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
[Scanners]
Port=1585
Header=Store #<StoreNum>
Footer=(c)2017 My Test Company Inc
Type=Serial
Read in the INI file C:\myinifile.ini using Get-IniContent, then pipe it to Remove-IniComment to uncomment 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.
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
```PowerShell
Get-IniContent "C:\myinifile.ini" | Remove-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