Please use DSCR_FileContent module.
PowerShell DSC Resource to create INI file.
You can install Resource from PowerShell Gallery.
Install-Module -Name DSCR_IniFile
- cIniFile PowerShell DSC Resource to create ini file.
-
[string] Ensure (Write):
- Specify the key exists or not.
- The default value is
Present
. (Present
|Absent
)
-
[string] Path (Key):
- The path of the INI file.
-
[string] Key (Key):
- Key element.
- If you specified key as empty string, cIniFile only check the section.
-
[string] Value (Write):
- The value corresponding to the key.
- If this param not specified, will set empty string.
-
[string] Section (Key):
- The section to which the key belongs.
- If the key doesn't need to belong section, you should set the value for an empty string.
-
[string] Encoding (Write):
- You can choose text encoding for the INI file.
UTF8NoBOM
(default) /UTF8BOM
/utf32
/unicode
/bigendianunicode
/ascii
-
[string] NewLine (Write):
- You can choose new line code for the INI file.
CRLF
(default) /LF
- Example 1: Sample configuration
Configuration Example1 {
Import-DscResource -ModuleName DSCR_IniFile
cIniFile Apple {
Path = "C:\Test.ini"
Section = ""
Key = "Fruit_A"
Value = "Apple"
}
cIniFile Banana {
Path = "C:\Test.ini"
Section = ""
Key = "Fruit_B"
Value = "Banana"
}
cIniFile Ant {
Path = "C:\Test.ini"
Section = "Animals"
Key = "Animal_A"
Value = "Ant"
}
}
The result of executing the above configuration, the following ini file will output to C:\Test.ini
Fruit_A=Apple
Fruit_B=Banana
[Animals]
Animal_A=Ant
Load ini file and convert to the dictionary object
- Syntax
Get-IniFile [-Path] <string> [-Encoding { <utf8> | <utf8BOM> | <utf32> | <unicode> | <bigendianunicode> | <ascii> | <Default> }]
Convert dictionary object to ini expression string
- Syntax
ConvertTo-IniString [-InputObject] <System.Collections.Specialized.OrderedDictionary>
- Example
PS> $Dictionary = [ordered]@{ Section1 = @{ Key1 = 'Value1'; Key2 = 'Value2' } }
PS> ConvertTo-IniString -InputObject $Dictionary
[Section1]
Key1=Value1
Key2=Value2
Set a key value pair to the dictionary
- Syntax
Set-IniKey [-InputObject] <System.Collections.Specialized.OrderedDictionary> -Key <string> [-Value <string>] [-Section <string>] [-PassThru]
- Example
PS> $Dictionary = [ordered]@{ Section1 = @{ Key1 = 'Value1'; Key2 = 'Value2' } }
PS> $Dictionary | Set-IniKey -Key 'Key2' -Value 'ModValue2' -Section 'Section1' -PassThru | ConvertTo-IniString
[Section1]
Key1=Value1
Key2=ModValue2
Remove a key value pair from dictionary
- Syntax
Remove-IniKey [-InputObject] <System.Collections.Specialized.OrderedDictionary> -Key <string> [-Section <string>] [-PassThru]
- Example
PS> $Dictionary = [ordered]@{ Section1 = @{ Key1 = 'Value1'; Key2 = 'Value2' } }
PS> $Dictionary | Remove-IniKey -Key 'Key2' -Section 'Section1' -PassThru | ConvertTo-IniString
[Section1]
Key1=Value1
-
3.1.0
- Export useful functions (
Get-IniFile
,ConvertTo-IniString
,Set-IniKey
,Remove-IniKey
) - Fix typo in the module name
- Export useful functions (
-
3.0.1
- Fix PSSA issues (PSAvoidTrailingWhitespace)
- Remove unnecessary files in the published data
-
3.0.0
- [BREAKING CHANGE] Default output encoding is changed from
UTF8BOM
toUTF8NoBOM
- [BREAKING CHANGE] Default output encoding is changed from
-
2.0.1
- Fixed issue that an incorrect INI file has been created when the encoding other than
UTF8NoBOM
was specified.
- Fixed issue that an incorrect INI file has been created when the encoding other than
-
2.0.0
- Added NewLine property for specifying the new line code. (
CRLF
orLF
) - You can now specify
UTF8NoBOM
as Encoding. - [DEPRECATED] Encoding options
UTF7
andBigEndianUTF32
have been deprecated.
- Added NewLine property for specifying the new line code. (
-
1.2.0
- Check section only when
Key = ""
#2
- Check section only when
-
1.1.1
- Fixed issue that does not work correctly when
Ensure = "Absent"
#1 Value
param is no longer mandatory.
- Fixed issue that does not work correctly when