Powershell - 输出目录递归到控制台和文件

时间:2014-08-29 09:06:22

标签: powershell

我正在使用以下代码将共享文件夹结构输出到文本文件:

$path = "\\server\share"
$file = "c:\share-folders.txt"
get-childitem "$path" -recurse -directory | select -expand fullname >> "$file"

我还想将它作为跟踪状态的方式同时写入控制台。我已经尝试使用tee-object附加文件然后管道写入主机,但最多我只能输出到文本文件或控制台,但不能同时输出。有谁知道完成这个的正确方法?


解决方案:将-Append添加到Tee-Object cmdlet的末尾解决了问题。

$path = "\\server\share"
$file = "c:\share-folders.txt"
Get-ChildItem "$Path" -Recurse -Directory | Select -Expand FullName | Tee-Object -FilePath "$File" -Append

2 个答案:

答案 0 :(得分:4)

这对我有用

get-childitem "$path" -recurse -directory |  select -expand fullname | %{ Write-Output $_ ; $_ >> "$file"}

在此之后,使用Tee命令行开关:

get-childitem "$path" -recurse -directory |   select -expand fullname | tee -file  "$file"

答案 1 :(得分:0)

或试试这个:
首先选择数据,然后将foreach对象添加到文本文件中并将其写入控制台。

$path = "\\server\share"
$file = "c:\share-folders.txt"

Get-ChildItem "$path" -Recurse -Directory | Select-Object -ExpandProperty FullName | ForEach-Object {Add-Content -Value $_ -Path $file; Write-Host -Object $_}
相关问题