-
Notifications
You must be signed in to change notification settings - Fork 52
Set IniContent
This function updates specified keys to new values in all sections or certain sections of an ini file. It can be used to either add new values or change existing ones. To comment, uncomment, or remove keys use those related functions instead.
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 saved to a file using 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.
This parameter specifies a Hashtable of one or more key names and values to modify. If they don't exist, they will be added. Type: HashTable This parameter must be provided.
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 sic
for the function.
Sample INI file:
[Printers]
Port=3023
Type=USB
Name With Space=SomeValue
Read in the INI file c:\myinifile.ini, add or update the 'Name With Space' and 'AnotherName' keys in the [Printers] section to the values specified, and save the modified ini to $ini.
$ini = Set-IniContent -FilePath "C:\myinifile.ini" -Sections 'Printers' -NameValuePairs @{'Name With Space' = 'Value1' ; 'AnotherName' = 'Value2'}
Sample INI content of $ini["Printers"] after changes. Note that this has not yet been saved to a file.
> Name Value
> ---- -----
> Port 3023
> Type USB
> Name With Space Value1
> AnotherName Value2
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 add or update the 'Updated' key in the [Terminals] and [Monitors] sections to the value specified. The ini is then piped to Out-IniFile to write the INI file to c:\myinifile.ini. If the file is already present it will be overwritten.
Set-IniContent -FilePath "C:\myinifile.ini" -Sections 'Terminals','Monitors' -NameValuePairs @{'Updated=FY17Q2'} | Out-IniFile "C:\myinifile.ini" -Force
Sample INI file after changes:
[Terminals]
Front=Reg1
DriveThru=Reg2
Updated=FY17Q2
[Monitors]
Kitchen=1
Fryer=2
Alternate=3
Updated=FY17Q2
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 Set-IniContent to add or update the 'Header' and 'Update' keys in all sections to the specified values. The ini is then piped to Out-IniFile to write the INI file to c:\myinifile.ini. If the file is already present it will be overwritten.
Get-IniContent "C:\myinifile.ini" | Set-IniContent -NameValuePairs @{'Header' = 'True' ; 'Update' = 'False'} | Out-IniFile "C:\myinifile.ini" -Force
Sample INI file after changes:
[Printers]
Port=3023
Header=True
Footer=(c)2017 My Test Company Inc
Type=USB
Update=False
[Scanners]
Port=1585
Header=True
Footer=(c)2017 My Test Company Inc
Type=Serial
Update=False
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 Set-IniContent to add or update the [Updated] key that is orphaned, i.e. not specifically in a section. The ini is then piped to Out-IniFile to write the INI file to c:\myinifile.ini.
Get-IniContent "C:\myinifile.ini" | Set-IniContent -NameValuePairs @{'Updated'='FY17Q2'} -Sections '_' | Out-IniFile "C:\myinifile.ini" -Force
Sample INI file after changes:
Updated=FY17Q2
[Terminals]
Front=Reg1
DriveThru=Reg2
Updated=1/1/2017
[Monitors]
Kitchen=1
Fryer=2
Alternate=3
Updated=11/25/2016