coding problems with Powershell script

Welcome Forums General PowerShell Q&A coding problems with Powershell script

Viewing 6 reply threads
  • Author
    Posts
    • #247495
      Participant
      Topics: 2
      Replies: 2
      Points: 18
      Rank: Member

      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

      • This topic was modified 1 month, 2 weeks ago by 2Maik2.
    • #247507
      Participant
      Topics: 5
      Replies: 328
      Points: 522
      Helping Hand
      Rank: Major Contributor

      You misspelled your text variable.

    • #247510
      Participant
      Topics: 8
      Replies: 558
      Points: 2,136
      Helping Hand
      Rank: Community Hero

      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.

      [/crayon] [/crayon]
    • #247516
      Participant
      Topics: 2
      Replies: 2
      Points: 18
      Rank: Member

      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

       

       

       

    • #248011
      Participant
      Topics: 2
      Replies: 2
      Points: 18
      Rank: Member

      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?

    • #249308
      Participant
      Topics: 0
      Replies: 7
      Points: 46
      Rank: Member

      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;

       

      I hope this helps.

    • #249362
      Participant
      Topics: 0
      Replies: 78
      Points: 348
      Helping Hand
      Rank: Contributor

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

Viewing 6 reply threads
  • You must be logged in to reply to this topic.