Xander Vane
Xander Vane

Reputation: 197

Replace a char in a text file using powershell

I'm trying to replace a part on a text file. First i searched if it is present in the text file

$check = Get-Content sample.txt | Where-Object {$_.Contains('<add key="IIS_VERSION" value="7" />')}

if ($checkIISv)
{
Write-Host "IIS's version is already 7"
}
else
{
Write-Host "Update IIS version to 7"

# I want to place here a code that will replace the value="7" if it is not =7
# This value is usually not 7

}

Please help, I dont think the replace function can be used here since the "value" is considered as unknown.

Upvotes: 0

Views: 297

Answers (4)

Xander Vane
Xander Vane

Reputation: 197

My problem now is that it it only updates the bar code format and no longer the iis version if it is lower than 7

$path ='C:\Users\hello\Documents'

#Update the IIS version to 7

$content = Get-Content $path\Web.config

if ($content -match '<add key="IIS_VERSION" value="7" />') 
{
  Write-Host "IIS's version is already 7"
} 
else 
{
  Write-Host "Updated IIS version to 7"
  $content -replace '<add key="IIS_VERSION" value="[^"]+" />', '<add key="IIS_VERSION" value="7" />' | Set-Content $path\sample.txt
}


# Update the Barcode Format

if ($content -match '<add key="BARCODE_FORMAT" value="1" />')
{
    Write-Host "Barcode format is already 1"
}
else
{
    Write-Host "Updated Barcode Format to 1"
    $content -replace '<add key="BARCODE_FORMAT" value="[^"]+" />', '<add key="BARCODE_FORMAT" value="1" />' | Set-Content $path\sample.txt
}

Upvotes: 0

Martin Brandl
Martin Brandl

Reputation: 58931

The content looks like XML so you should consider using an xpath expression to select the node and change the value (See the answer from Frode F.). However, you can also do this with a regex (using a positive lookbehind to find the position)

$txt = Get-Content sample.txt
$regex = '(?<=<add key="IIS_VERSION" value=")[^"]+'
$txt -replace $regex, 7
$txt | Set-Content sample.txt

Upvotes: 0

Joey
Joey

Reputation: 354486

If the line is always there you can of course use -replace. It does a regex replace, which can be used here:

$content = Get-Content sample.txt
if ($content -match '<add key="IIS_VERSION" value="7" />') {
  Write-Host "IIS's version is already 7"
} else {
  Write-Host "Update IIS version to 7"
  $content -replace '<add key="IIS_VERSION" value="[^"]+" />', '<add key="IIS_VERSION" value="7" />'
}

That's about the simplest way of doing that when using a string replace.

However, your file looks very much like XML. In which case it should actually be easier to use XML manipulation, which PowerShell offers as well:

$content = [xml](Get-Content sample.txt)
$iisKey = $content.SelectSingleNode("//add[@key = 'IIS_VERSION']")
if ($iisKey.value -eq 7) {
  Write-Host "IIS's version is already 7"
} else {
  Write-Host "Update IIS version to 7"
  $iisKey.value = '7'
  $content.Save((Resolve-Path sample.txt))
}

(Untested, and I don't do that often, but should be something alone those lines.)

Upvotes: 1

Frode F.
Frode F.

Reputation: 54881

You could use -replace with a regex-pattern to replace the value. However, this is an web/app xml-config so you should modify it as an XML-document. Ex.

#$xml = [xml](Get-Content "c:\folder\app.config")
$xml = [xml]@"
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="IIS_VERSION" value="8" />
    <add key="somekey" value="somevalue" />
  </appSettings>
</configuration>
"@

$IISVersion = $xml.SelectSingleNode("//add[@key='IIS_VERSION']")

if ($IISVersion.Value -eq 7)
{
    Write-Host "IIS's version is already 7"
}
else
{
    Write-Host "Update IIS version to 7"
    $IISVersion.Value = "7"
}

$xml.Save("c:\folder\app.config")

Upvotes: 2

Related Questions