Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell INI editing

I want to changing some values in an INI file. Unfortunately, I have keys in 2 different sections which share an identical name but need different values. My code uses the Get-IniContent function from PsIni.

Example INI file:

[PosScreen]
BitmapFile=C:\Temp\Random.bmp
Bitmap=1

[ControlScreen]
BitmapFile=C:\Temp\Random.bmp
Bitmap=1 

I need to change the above to the following:

[PosScreen]
BitmapFile=C:\Temp\FileC.bmp
Bitmap=1

[ControlScreen]
BitmapFile=C:\Temp\FileD.bmp
Bitmap=1 

The PowerShell code I am using seems to work, but it changes every value to "File D". It is obviously parsing everything twice, and the name is the same for each section.

$NewFileC = "C:\Temp\FileC.bmp"
$NewFileD = "C:\Temp\FileD.bmp"
$POSIniContent = Get-IniContent "C:\scripts\Update-EnablerImage\WINSUITE.INI"
$BOIniContent = Get-IniContent "C:\scripts\Update-EnablerImage\WINSUITE.INI"

If ($POSIniContent["PosScreen"]["BitmapFile"] -ne $NewFileC) {
  Get-Content "C:\scripts\Update-EnablerImage\WINSUITE.INI" |
    ForEach-Object {$_ -replace "BitmapFile=.+" , "BitmapFile=$NewFileC" } |
    Set-Content "C:\scripts\Update-EnablerImage\WINSUITE.INI"
}

If ($BOIniContent["ControlScreen"]["BitmapFile"] -ne $NewFileD) {
  Get-Content "C:\scripts\Update-EnablerImage\WINSUITE.INI" |
    ForEach-Object {$_ -replace "BitmapFile=.+" , "BitmapFile=$NewFileD" } |
    Set-Content "C:\scripts\Update-EnablerImage\WINSUITE.INI"
}

My struggle is how to change each one independently. I'm a bit of a scripting newbie, so calling out for some help. Tried using Conditional Logic (ForEach $line in $INIFile, for example), but no luck with that.

like image 584
Pete Symonds Avatar asked Apr 02 '14 05:04

Pete Symonds


People also ask

How do I edit windows PowerShell script?

Opening and Editing ScriptsRight-click on the PowerShell script in Windows Explorer and choose Edit. Use the psEdit command (which is actually a predefined function) in the command window and specify the path and filename to the script.

How do I replace a string in a PowerShell file?

Finding and Replacing the String One way to do that is to use the -replace operator. This PowerShell operator finds a string and replaces it with another. Using the example file contents, we can provide the search string foo with the replacement string bar which should make the file contents foo foo baz now.

Where is PowerShell config file?

A powershell. config. json file in the $PSHOME directory defines the configuration for all PowerShell sessions running from that PowerShell installation. The $PSHOME location is defined as the same directory as the executing System.


1 Answers

You are overcomplicating things. You can use Get-IniContent and Out-IniFile as follows:

$ini = Get-IniContent c:\temp\ini.ini 
$ini["posscreen"]["BitmapFile"] = "C:\Temp\FileC.bmp"  
$ini | Out-IniFile -FilePath c:\temp\ini2.ini

Note that if you want to overwrite the original file, you must add -Force to the Out-IniFile call.

like image 127
Loïc MICHEL Avatar answered Oct 04 '22 00:10

Loïc MICHEL