Search code examples
powershellformatiniediting

INI file editing, Save format


So i have a working code. i just learned about this today but am wondering if there is a way to save the formatting. (found it on here btw).

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

This is how it looks normally:

 [ShapePageFnt]
 Position=1.73 -2.76 -15.00
 Scale=0.35 0.36 0.38
 ZoneDepth=0
 StringLength=9600
 Font=VerdanaBold
 Color=0xFF000000
 Kerning=0.270000
 String=1/10
 StringAlignment=Left 

 [sgcArialBlack]
 FontFile=ArialBlack.png
 DataFile=ArialBlack.ftd
 StringLength=0
 StringStack=0
 StringAlignment=Center
 Kerning=0.25
 Color=0xFF00AAFF
 Position=0 0 -1000
 Dimension=1 1
 Scale=1 1 1
 String=

 [sgcXtraLabel]
 Position=-3.25 -5.61 -15.00
 Dimension=1.00 1.00
 Scale=0.33 0.33 0.33
 ZoneDepth=-101
 StringLength=20
 Font=VerdanaBold
 Color=0xFFFFFFFF
 String=Xtra Games
 StringAlignment=Center

After running the code:

[ShapePageFnt]
Position=1.73 -2.76 -15.00
Scale=0.35 0.36 0.38
ZoneDepth=0
StringLength=9600
Font=VerdanaBold
Color=0xFF000000
Kerning=0.270000
String=1/10
StringAlignment=Left 
[sgcArialBlack]
FontFile=ArialBlack.png
DataFile=ArialBlack.ftd
StringLength=0
StringStack=0
StringAlignment=Center
Kerning=0.25
Color=0xFF00AAFF
Position=0 0 -1000
Dimension=1 1
Scale=1 1 1
String=
[sgcXtraLabel]
Position=-3.25 -5.61 -15.00
Dimension=1.00 1.00
Scale=0.33 0.33 0.33
ZoneDepth=-101
StringLength=20
Font=VerdanaBold
Color=0xFFFFFFFF
String=Xtra Games
StringAlignment=Center

Any help would be appreciated.

** i tried -format and -table just for kicks and didn't do anything but pop up errors.** Is there a /? for stuff in power shell to check if these conditions are even available?

End goal: to have the code ran and have it output the same way it inputs with the spaces. (the code replaces a set item in the .ini)


Solution

  • I downloaded the script and had a look at the parameters for the Out-IniFile function. I found two switch parameters there:

    • Pretty which adds an extra linebreak between Sections
    • Loose which adds spaces around the equal sign when writing the key = value

    To use them, your command to write the ini file would be:

    $ini | Out-IniFile -FilePath c:\temp\ini2.ini -Pretty -Loose
    

    The author of that script forgot to add descriptions of the parameters in the comment-based help info, so that's why Get-Help didn't show it..