none
How to insert a newline before adding content with add-content

    Question

  • Hello,

    To insert a blank line in my output file before I perform the add-content cmdlet, can the 'n special characters be used somewhere in the cmdlet as below (which doesnt work) or need I just execute another add-content cmdlet to add a blank line.

    add-content -path myfile.txt -value `n"My text"

    add-content -path myfile.txt -value " ";add-content -path myfile.txt -value "My text"


    Thanks for your help! SdeDot

    Monday, February 27, 2012 1:21 PM

Answers

  • add-content -path myfile.txt -value "`r`nMy text"

    • Proposed as answer by Bigteddy Monday, February 27, 2012 1:31 PM
    • Marked as answer by SdeDot Monday, February 27, 2012 1:34 PM
    Monday, February 27, 2012 1:25 PM

All replies

  • add-content -path myfile.txt -value "`r`nMy text"

    • Proposed as answer by Bigteddy Monday, February 27, 2012 1:31 PM
    • Marked as answer by SdeDot Monday, February 27, 2012 1:34 PM
    Monday, February 27, 2012 1:25 PM
  • Ahh, so the trick is a carriage return first....Thanks MichalGajda!

    Thanks for your help! SdeDot

    Monday, February 27, 2012 1:35 PM
  • Ahh, so the trick is a carriage return first....Thanks MichalGajda!

    Thanks for your help! SdeDot


    No. The trick is that the `n newline goes inside the double quotes.

    Al Dunbar

    Monday, February 27, 2012 2:09 PM
  • yeah, a single quote is REALLY verbatim... which kinda stinks.. I wish it
    would handle escaped chars..
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, February 27, 2012 3:25 PM
  • yeah, a single quote is REALLY verbatim... which kinda stinks.. I wish it
    would handle escaped chars..

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If it did understand escaped chars, how would you enter a literal ` (backtick) ?

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Monday, February 27, 2012 5:13 PM
  • like most languages that escape things.....  ``
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, February 27, 2012 5:15 PM
  • Ironically, and much to your dismay, I'm sure, Justin, using double-quotes you can escape the escape character in the normal way:

    "``"

    Prints:

    `


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Monday, February 27, 2012 5:22 PM
  • there is no dismay :)
     
    what im saying is that I think with double quotes it should eval $ (and `)
    but with single quotes it should only eval `
     
    the reason for this is that there are very few escape sequences and you
    would likely want most of them in a string, but then again the primary one
    that comes to mind is new line, and in that case, why not use a single quote
    here string?
     
    just threw me for a loop the first time I stumbled across it...
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, February 27, 2012 5:28 PM
  • there is no dismay :)
    what im saying is that I think with double quotes it should eval $ (and `)
    but with single quotes it should only eval `
    the reason for this is that there are very few escape sequences and you
    would likely want most of them in a string, but then again the primary one
    that comes to mind is new line, and in that case, why not use a single quote
    here string?
    just threw me for a loop the first time I stumbled across it...

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    I am sure this (i.e. both listeral string formats) throws everyone for a loop at one time or another... ;-)

    It seems to have been a design decision that a single-quoted string would be completely literal, with the double-quoted string allowing the use of special excaped characters, variables, and even expressions. Personally, I think it was a good design decision as it relieves us of having to remember precisely what will be processed and what will re used literally.

    It also allows you to create a string constant that contains the escape character without having to double it, even though the backtick character is rarely used in text outside of powershell code. ;-)

    If, as you say, the primary escape sequence is newline, well, you can easily embed that in a single-quoted herestring. Or, if you want your code to assign to a variable a literal string containing no newline characters that is sooo long it would look ugly to have it on one line and you want to break it for readability, try this:

    PS C:\> $longline = @'
    >> this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >>  this is a very long string,
    >> '@ -replace "`n",""
    >> $longline
    >>
    this is a very long string, this is a very long string, this is a very long string, this is a very long string, this is
     a very long string, this is a very long string, this is a very long string, this is a very long string, this is a very
     long string, this is a very long string, this is a very long string, this is a very long string, this is a very long s
    tring, this is a very long string, this is a very long string, this is a very long string, this is a very long string,
    this is a very long string, this is a very long string, this is a very long string, this is a very long string, this is
     a very long string, this is a very long string, this is a very long string, this is a very long string, this is a very
     long string, this is a very long string,
    PS C:\>


    Al Dunbar

    Monday, February 27, 2012 10:14 PM
  • Ironically, and much to your dismay, I'm sure, Justin, using double-quotes you can escape the escape character in the normal way:

    "``"

    Prints:

    `


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    And further to that, you just have to remember that you never need do any escaping in a single-quoted string, but in a double-quoted string you will need to if there are any special characters you want to have appear literally. Or, conversely, if you want *any* expansion or substitution to take place, you need to use double-quotes but remember to escape those special characters you want to have appear literally.

    If it worked the way Justin thought would be more logical, the above rule would be just a bit more complex.


    Al Dunbar

    Monday, February 27, 2012 10:24 PM