coding problems with Powershell script

Hello,

I have a problem with the coding and the processing/handling with my script.

The coding of my PS1 sript is UTF8. I load a text file (also UTF8) into my powershell script and read some content like

$inserttext= “C:\user\insert\mytext.txt”

$insert = Get-Content -path $inserttext | where {$_ -match “Home-Nr.:”}

$insert = ($insert).replace(“Home-Nr” , “”)

($insert = “MainStreet Home-Nr.: 83”)

There is the problem with the coding, because the replace option does not work. Nothing happend - got the same output like before or the “get-content” function does not work. I think in the backgroud the “Home-Nr.:” has some different/special letters.

Does anyone have an idea?

Thanks in advance

 

Edit: variable

You misspelled your text variable.

$inserttext = “C:\user\insert\mytext.txt”
$insert = Get-Content -path $inserttext | Where-Object {$_ -match “Home-Nr.:”}
$replace = $insert -replace “Home-Nr” ; $replace

Are you trying to actually update the file? In it’s current form, it will only edit that line in memory. The previous answer should help you get the modified line in the variable. Here are two examples of how you can modify the line and update the file. For the sake of other readers I have provided a test text file.

$file = "C:\temp\textfile.txt"

@'
some text
Home-NR.:
more text
'@ | Out-File $file -Encoding utf8

$insert = Get-Content -path $file -Encoding utf8

$insert = $insert | Foreach {
    $_ -replace 'Home-Nr.:', “MainStreet Home-Nr.: 83”
}

$insert | Out-File $file -Encoding utf8
$file = "C:\temp\textfile.txt"

@'
some text
Home-NR.:
more text
'@ | Out-File $file -Encoding utf8

$insert = Get-Content -path $file -Encoding utf8

$insert -replace 'Home-Nr.:', “MainStreet Home-Nr.: 83” | Out-File $file -Encoding utf8

Hello,

thanks, I have corrected it - but in my script all is fine - just a failure by threat creation.

you use a differt syntax like - I have to check this: $replace = $insert -replace “Home-Nr” ; $replace

 

@Doug: when the script has finished, the content of the variable will be stored in another txt-file - which works fine.
Is just the fact, that the content of the line is not replaced correct. I will also check your code

 

 

 

Ok, i have solved the issue of reading out the variable.

 

I have antoher questiosn.

I need a conversion of my text file - it’s in UTFf8 without BOM I need it in UTF-8 with BOM. I can use powershell or inconv

Does anyone have any idea how this works best?

I found this blog post about removing BOM https://debay.blog/2019/10/03/powershell-utf8-and-bom

Reversing the logic theend of you script could be;

# original ending
$insert | Out-File $file -Encoding utf8


# change to
$UTF8BOM = New-Object System.Text.UTF8Encoding($true)
[System.IO.File]::WriteAllLines($file, $insert, $UTF8BOM)

 

I hope this helps.

Windows PowerShell defaults to using BOM when UTF8 is used as encoding for file writing cmdlets. PowerShell Core defaults to UTF8 without BOM.

# Windows PowerShell
$data | Set-Content FileWithBOM.txt -Encoding UTF8

# PowerShell Core
$data | Set-Content FileWithBOM.txt -Encoding UTF8BOM