blank line using powershell

Welcome Forums General PowerShell Q&A blank line using powershell

This topic contains 8 replies, has 3 voices, and was last updated by

 
Participant
2 months, 2 weeks ago.

  • Author
    Posts
  • #163869

    Participant
    Topics: 10
    Replies: 48
    Points: 200
    Rank: Participant

    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: 1
    Replies: 1529
    Points: 2,587
    Helping Hand
    Rank: Community Hero

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

  • #163908

    Participant
    Topics: 10
    Replies: 48
    Points: 200
    Rank: Participant

    $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: 1
    Replies: 1529
    Points: 2,587
    Helping Hand
    Rank: Community Hero

    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: 10
    Replies: 48
    Points: 200
    Rank: Participant

    I mean any alternate way to achieve this?

  • #163944

    Participant
    Topics: 1
    Replies: 1529
    Points: 2,587
    Helping Hand
    Rank: Community Hero

    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: 10
    Replies: 48
    Points: 200
    Rank: Participant

    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: 1
      Replies: 1529
      Points: 2,587
      Helping Hand
      Rank: Community Hero

      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: 999
    Points: 1,946
    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

     

     

     

     

     

     

     

     

You must be logged in to reply to this topic.