Powershell附加到输出

1 powershell append

我正在“自学Powershell”,并且已经收获颇丰,而google /此网站尚未使我能够找到解决方案。我正在使用来自不同目录的文件列表来编译文本文件,但是在将新数据附加到文件时遇到了麻烦。

get-childitem $dir -recurse | % {write-output $_.fullname} >$file
Run Code Online (Sandbox Code Playgroud)

创建我的文件,但随后我想从下面追加新记录

get-childitem $dir2 -recurse | % {write-output $_.fullname} >$file
Run Code Online (Sandbox Code Playgroud)

我已经尝试了add-content和-append,但是我无法弄清楚我没有做正确的事情。

Ign*_*tus 5

尝试:

get-childitem $dir -recurse | % {write-output $_.fullname} >> $file
Run Code Online (Sandbox Code Playgroud)

(经过测试和工作)

>>精度使其始终附加,>每次均覆盖一个。

或更改语法以使用Out-File

get-childitem $dir -recurse | % {write-output $_.fullname} | out-file -filepath $file -Append
Run Code Online (Sandbox Code Playgroud)

(未试)

在这种情况下,变量$file必须包含完整路径。喜欢:C:\directory\filename.txt