I want to edit the value of INI file. I use this script but It gives me error.
Get-IniContent : The term 'Get-IniContent' is not recognized as the name of a
cmdlet, function, script file, or operable program.
The contents of my INI file at c:\Users\file.ini
:
[XXX]
AB=23
BC=34
The contents of the script for reading and updating it:
# Read the content of an *.ini file into a (nested) hashtable.
$ini = Get-IniContent "C:\Users\file.ini"
# Update the 'AB' entry in section [XXX] in-memory.
$ini["XXX"]["AB"] = "12"
# Write the updated content back to the *.ini file.
$ini | Out-IniFile -FilePath "C:\Users\file.ini -Force"
JeroenMostert has provided the crucial pointer in a comment:
PowerShell, as of v6.2.0, has no built-in cmdlets for processing INI files (
*.ini
), though introducing such cmdlets is being discussed on GitHub.Get-IniContent
andOut-IniFile
are advanced functions (cmdlet-like functions) that come with the third-partyPSIni
module, available from the PowerShell Gallery.In PowerShell v5 or higher, which comes with the
PowerShellGet
module[1], installation is as easy as:If you omit
-Scope CurrentUser
, you'll install the module for all users, but doing so requires running with administrative privileges.With
$PSModuleAutoLoadingPreference
at its default (unset), this module is then loaded automatically, on demand into a session that tries to call one of the module's commands, such asGet-IniContent
.Here's a complete, self-contained example that exercises the core functionality of the
PsIni
module:*.ini
file from scratch withOut-IniFile
, from a nested ordered hashtable.Get-IniContent
into a (new) nested ordered hashtableOut-IniFile
Note: The assumptions are that
Install-Module
is available, i.e., that thePowerShellGet
module is installed, and that the running machine is online and permitted to download packages from https://www.powershellgallery.com/.Running the above should succeed and output the following, demonstrating that the
*.ini
file was successfully created, read back into memory, modified, and saved back to disk:[1] You can install
PowerShellGet
on demand for PowerShell versions 3 and 4 - see https://www.microsoft.com/en-us/download/details.aspx?id=51451