In cmd.exe
I can type copy con <filename>
and write whatever to file, or com1, for example. Can I do same things in powershell?
You can use a here-string and redirect it to a file:
@'
type
your
lines
here
'@ > file.txt
Note:
Both the opening ( @'
) and the closing delimiter ( '@
) must be on their own lines and that the closing delimiter must be at the very start of the line.
@"
and "@
instead, you get an expandable here-string, ie on with string interpolation, allowing you to reference variables and embed expressions and command output. Interactively, every line but the first will be prefixed with the line-continuation prompt string, >>
, which I've omitted for conceptual clarity above.
Note: As postatnote points out, you don't strictly need a here-string for this, as even regular '...'
(verbatim) and "..."
(interpolating) strings can span multiple lines, but use of here-strings has two distinct advantages:
You needn't worry about escaping '
or "
in the content of the string.
Having the opening and closing delimiters on their own line makes for clearer visual separation (if you tried '<newline>type...'
, for instance, the newline would become part of the content).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.