-
Notifications
You must be signed in to change notification settings - Fork 52
Remove IniEntry
This function removes 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.
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 rie
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
Version=2.1
Read in the INI file c:\myinifile.ini, remove any keys named 'Header' or 'Version' in the [Printers] section, and save the modified ini to $ini.
$ini = Remove-IniEntry -FilePath "C:\myinifile.ini" -Sections 'Printers' -Keys 'Header','Version'
Sample INI content of $ini["Printers"] after changes. Note that this has not yet been saved to a file.
> Name Value
> ---- -----
> Port 3023
> 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 remove 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-IniEntry -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
[Monitors]
Kitchen=1
Fryer=2
Alternate=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
[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-IniEntry to remove 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-IniEntry -Keys 'Header' | Out-IniFile "C:\myinifile.ini" -Force
Sample INI file after changes:
[Printers]
Port=3023
Footer=(c)2017 My Test Company Inc
Type=USB
[Scanners]
Port=1585
Footer=(c)2017 My Test Company Inc
Type=Serial
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 using Get-IniContent, then pipe it to Remove-IniEntry to remove the 'Terminals' 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-IniEntry -Sections 'Terminals' | Out-IniFile "C:\myinifile.ini" -Force
Sample INI file after changes:
[Monitors]
Kitchen=1
Fryer=2
Alternate=3
Updated=11/25/2016
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 Remove-IniEntry to remove 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" | Remove-IniEntry -Keys 'Updated' -Sections '_' | 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