Skip to content

$cs.Ini_ReadEntry

Reads a value from a classic INI file. Returns an empty string if the section/variable combination isn’t found in the file. The maximum value length that can be read is 255 characters.

$cs.Ini_ReadEntry(string filepath, string section, string variable)

Path to the INI file. Throws a FileNotFoundException if this file doesn’t exist.

Section in the INI file to read from.

Variable name under the section to read.

String, the value read, or an empty string if not found.

Read a migration flag to decide whether old settings still need to be migrated:

Terminal window
$iniFile = "$env:ProgramData\Contoso\App\app.ini"
$version = $cs.Ini_ReadEntry($iniFile, "migration", "firstRunVersionFlag")
$cs.Job_WriteLog("firstRunVersionFlag is currently: $version")

Read a value and fall back to a sensible default when it comes back empty (not found):

Terminal window
$iniFile = Join-Path $env:ProgramData "Contoso\App\settings.ini"
$logLevel = $cs.Ini_ReadEntry($iniFile, "logging", "level")
if ([string]::IsNullOrEmpty($logLevel)) {
$logLevel = "Info"
$cs.Job_WriteLog("logLevel not set in config, defaulting to '$logLevel'")
}

$cs.Ini_WriteEntry