【问题标题】:"copy con" or "type con > " equivalent in Powershell?Powershell中的“copy con”或“type con >”等价物?
【发布时间】:2017-06-23 13:46:39
【问题描述】:

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

copy con file.txt
Hello World
^Z

或者:

type con > file.txt
Hello World
^Z

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

【问题讨论】:

  • cmd /c copy con file.txt
  • @Bill_Stewart - 是的,这行得通,但感觉像是作弊。 :)
  • 骗子:notepad file.txt 也很方便,因为它会显示文件编码。
  • 我对你不使用 notepad.exe 的理由很感兴趣 :)

标签: powershell inline-editing


【解决方案1】:

将内容传送到out-file cmdlet 以模拟此操作。

"Hello World" | out-file hello.txt

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

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

点击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

【讨论】:

  • 更好。输出(在 hello2.txt 中)是否包含换行符?
  • @JeffZeitlin 是的
  • 适用于控制台版本,不适用于 ISE。抓住重点。 :)
  • 作为替代方案,使用@'<CR> (multiline string) <CR>'@ 提供包含任何符号的字符串数据,包括引号、撇号、反斜杠和美元符号。 “”是换行符。
  • 下划线好像有问题。他们都从结果中被淘汰了。
猜你喜欢
  • 1970-01-01
  • 2011-01-09
  • 2018-10-05
  • 2016-10-03
  • 2015-02-23
  • 1970-01-01
  • 2021-08-31
  • 2010-09-06
  • 2010-09-06
相关资源
最近更新 更多