如何将文件移动到另一个目录并在 Powershell 的一行中使用管道更改文件的内容?

How to move a file to another directory and change the content of it using pipe in one line in Powershell?

以下代码无效。但是在两条线上分别做。

移动项目 file.txt \相同目录 | Set-Content -Path \same-directory -Value "New content"

没有开关的 Move-Item cmdlet -PassThru returns 什么都没有,所以在那种情况下,没有任何东西通过管道发送到 Set-Content cmdlet。

如果你真的想做单行,使用

Move-Item -Path 'D:\file.txt' -Destination 'D:\some-directory' -PassThru | Set-Content -Value "New content"

由于现在我们实际上是在管道移动的对象,因此需要省略 Set-Content-Path 参数。

当然,目标文件夹D:\some-directory需要存在