replacing and adding values in a file

I Have a file which may be empty or may contain one or two or all the three of the following key value pairs:

===================

var1 = “First”,

Var2 = “second”,

Var3 = 30

===================

A part of my PowerShell script needs to take inputs of one or two or three of the below three variables

$Var1 = “Fifth”

$Var2 = “sixth”

$Var3 = 40

The condition is , if the input variable is present in the file then just replace the value of it. For example if var1 is present in the file then replace the value of var1 with “Fifth” If it’s not present in the file then append a new line at the end. While appending the new line a “,” needs to be added to the previous line. The file should not be overridden, only the value should be replaced or added as a new line.

 

Hi Afreen,

Did you have some powershell code that you are needing help with?

Sample Code:

# Sample code to update input file to reflect script variables

#region Scenario 1 - file with 3 variables, script with 3 variables ==> expected outcome update 3 variables in file

# Sample input file
@'
var1 = “First”,
Var2 = “second”,
Var3 = 30
'@ | Out-File .\testinput1.txt

# Sample script variables
$Var1 = “Fifth”
$Var2 = “sixth”
$Var3 = 40
#endregion

#region Scenario 2 - file with 2 variables, script with 3 variables ==> expected outcome update 2 variables in file, add 3rd

# Sample input file
@'
var1 = “First”,
Var2 = “second”
'@ | Out-File .\testinput1.txt

# Sample script variables
$Var1 = “Fifth”
$Var2 = “sixth”
$Var3 = 40
#endregion

#region Scenario 3 - file with 3 variables, script with 2 variables ==> expected outcome update 2 variables in file, ignore 3rd

# Sample input file
@'
var1 = “First”,
Var2 = “second”,
Var3 = 30
'@ | Out-File .\testinput1.txt

# Sample script variables
$Var1 = “Fifth”
Remove-Variable Var2 -EA 0 
$Var3 = 40
#endregion

#region Processing
'###############################################'
'Starting File content:'
Get-Content .\testinput1.txt
' '
'Script variables:'
if ($Var1) { "Var1 = $Var1" }
if ($Var2) { "Var2 = $Var2" }
if ($Var3) { "Var3 = $Var3" }
' '

$UpdatedData = foreach ($Line in (Get-Content .\testinput1.txt)) {
    switch ($Line) {
        { $_ -match 'Var1' } { if ($Var1) {"var1 = ""$Var1"","} else {$Line} }
        { $_ -match 'Var2' } { if ($Var2) {"var2 = ""$Var2"","} else {$Line} }
        { $_ -match 'Var3' } { if ($Var3) {"var3 = ""$Var3"","} else {$Line} }
        default              { $Line }
    }
}

# Add missing variables if any:
if ($Var1 -and -not (($UpdatedData -match 'Var1'))) { $UpdatedData += "var1 = ""$Var1""," }
if ($Var2 -and -not (($UpdatedData -match 'Var2'))) { $UpdatedData += "var2 = ""$Var2""," }
if ($Var3 -and -not (($UpdatedData -match 'Var3'))) { $UpdatedData += "var3 = ""$Var3""," }

$UpdatedData[-1] = $UpdatedData[-1] -replace ',','' # remove comma from last line
$UpdatedData | Out-File .\testinput1.txt            # Update the file

# Validation
'File content now:'
Get-Content .\testinput1.txt
' '
#endregion

Sample Output:

###############################################
Starting File content:
var1 = “First”,
Var2 = “second”,
Var3 = 30
 
Script variables:
Var1 = Fifth
Var2 = sixth
Var3 = 40
 
File content now:
var1 = "Fifth",
var2 = "sixth",
var3 = "40"
 
###############################################
Starting File content:
var1 = “First”,
Var2 = “second”
 
Script variables:
Var1 = Fifth
Var2 = sixth
Var3 = 40
 
File content now:
var1 = "Fifth",
var2 = "sixth",
var3 = "40"

###############################################
Starting File content:
var1 = “First”,
Var2 = “second”,
Var3 = 30
 
Script variables:
Var1 = Fifth
Var3 = 40
 
File content now:
var1 = "Fifth",
Var2 = “second”,
var3 = "40"

Hi Sam,

Thanks for the detailed script. I tried your script and it worked perfectly well. I had to add this sub part to my main script and was struck at it. Thanks a lot :slight_smile:

Hi Doug,

Yes, I did. Sorry if my question was lacking clarity. It’s all good now.

Thanks