blank line using powershell

Welcome Forums General PowerShell Q&A blank line using powershell

Viewing 7 reply threads
  • Author
    Posts
    • #163869
      Participant
      Topics: 15
      Replies: 68
      Points: 306
      Rank: Contributor

      I want to append some text to a file and after I append the text using Set-Content, I want to also append a blank line to the file. How can I append a blank line to a file using powershell?

    • #163890
      Participant
      Topics: 5
      Replies: 2368
      Points: 5,987
      Helping Hand
      Rank: Community MVP

      Either you add a space charachter or a line break / new line charachter. Please share the code you have so far.

    • #163908
      Participant
      Topics: 15
      Replies: 68
      Points: 306
      Rank: Contributor

      $eline=”`r”

      $text=”Testing”

      $myfile=Get-Content “C:\myfile.txt”

      Set-Content -Path “C:\myfile.txt” -value $text,$eline,$myfile

      This is working. However is there a better way to do this?

    • #163929
      Participant
      Topics: 5
      Replies: 2368
      Points: 5,987
      Helping Hand
      Rank: Community MVP

      This is working. However is there a better way to do this?

      What do you mean when you say “better way”? If it works …

    • #163932
      Participant
      Topics: 15
      Replies: 68
      Points: 306
      Rank: Contributor

      I mean any alternate way to achieve this?

    • #163944
      Participant
      Topics: 5
      Replies: 2368
      Points: 5,987
      Helping Hand
      Rank: Community MVP

      Probably there are several alternative ways but without knowing more of the actual task or environment and purpose I cannot see the advantage to change a working approach.

    • #163953
      Participant
      Topics: 15
      Replies: 68
      Points: 306
      Rank: Contributor

      I was wondering why I cannot use the new line character \n to insert a blank line. Because when I tried to use it, it printed \n as it is in the file.

      • #163965
        Participant
        Topics: 5
        Replies: 2368
        Points: 5,987
        Helping Hand
        Rank: Community MVP

        It depends on the operating system and on the editor or application how a line break / new line is treated / interpreted. If you have unix/linux files for example they look different in Windows. Usually in Windows a new line is represented by “`n`r” (new line, carriage return).

    • #163989
      Participant
      Topics: 2
      Replies: 1013
      Points: 2,093
      Helping Hand
      Rank: Community Hero

      This…

      \n

      … is not how you do the new line. It’s this…

      "`n"
      'Hello world'
      "`nWelcome"

       

      http://www.neolisk.com/techblog/powershell-specialcharactersandtokens

       

       

       

       

       

       

       

       

Viewing 7 reply threads
  • The topic ‘blank line using powershell’ is closed to new replies.