When working with Microsoft Deployment Toolkit (MDT) you often want to add stuff to, change or read CustomSettings.ini in a way or another.
To change a value of a property in an ini file you can do this in many ways.
You can install a module, you can replace a whole string with another or you can search for a property and set a new value.
If you know the whole string like “TaskSequenceID=REF-RCPOC03” it’s easy to just replace it.
But If the value like “REF-RCPOC03” can be whatever then you need to search for the property and then split the string and just get the value.
And then replace the string with the property + “=” + value.
# Get current value of a property
$currentValue = ((Select-String -Path "$($FilePath)\CustomSettings.ini" -pattern "$($property)\b" -AllMatches -ErrorAction Stop).ToString()).split("=")
# Replace it
(Get-Content "$($FilePath)\CustomSettings.ini").replace("$($property)=$($currentValue)", "$($property)=$($Value)") | Set-Content "$($FilePath)\CustomSettings.ini"
The easiest way is to create functions for this to Add, Set and Get.
3 of my CustomSettings.ini functions can be found on my PowerShell GitHub repo.
Okay, this will NOT work okay if you have a more complex customsettings.ini with lots of sections AND same property under more then one section.
But it’s an easy way if you have a small standard customsettings.ini for REF image creation or something like that.