powershellinline-editing

"copy con" or "type con > " equivalent in PowerShell?


On Command Prompt, and its syntactical ancestor, DOS, you can create a text file inline by doing this:

copy con file.txt
Hello World
^Z

Or:

type con > file.txt
Hello World
^Z

Is there an equivalent command in Powershell? Neither of the two commands I listed above work.


Solution

  • Pipe content to the out-file cmdlet to emulate this.

    "Hello World" | out-file hello.txt
    

    To get multiple lines, open the quotes but don't close them right away

    "hello
    >> is it me you're looking for" | out-file hello2.txt
    

    The >> will appear on the second line after hitting enter

    Another way is using "here-strings" for this instead of opening quotes.

    @'
    hello
    is it me you're looking for?
    I can even use ' @ $blabla etc in here
    and no substitutes will be placed
    You want var substitutes, use @"..."@ instead of @'...'@
    '@ | out-file hello.txt