“ copy con”或“ type con>”在Powershell中是否等效?

Gov*_*mar 5 powershell inline-editing

在命令提示符下,您可以通过以下方式内联创建文本文件:

copy con file.txt
Hello World
^Z
Run Code Online (Sandbox Code Playgroud)

要么:

type con > file.txt
Hello World
^Z
Run Code Online (Sandbox Code Playgroud)

在Powershell中有等效的命令吗?我上面列出的两个命令都不起作用。

alr*_*roc 7

通过管道将内容传递到out-filecmdlet可以对此进行仿真。

"Hello World" | out-file hello.txt
Run Code Online (Sandbox Code Playgroud)

要获得多行,请打开引号,但不要立即将其关闭

"hello
>> is it me you're looking for" | out-file hello2.txt
Run Code Online (Sandbox Code Playgroud)

点击>>后将出现在第二行enter

另一种方法是为此使用“ here-strings”而不是引号。

@'
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
Run Code Online (Sandbox Code Playgroud)