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

标签 powershell

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

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

我还想将其同时写入控制台,以跟踪状态。我尝试使用tee-object附加文件,然后通过管道将其传输到写入主机,但充其量我只能输出到文本文件或控制台,但不能一次输出。有谁知道实现此目标的正确方法?

解决方案:添加-附加到Tee-Object cmdlet的末尾解决了该问题。
$path = "\\server\share"
$file = "c:\share-folders.txt"
Get-ChildItem "$Path" -Recurse -Directory | Select -Expand FullName | Tee-Object -FilePath "$File" -Append

最佳答案

这对我有用

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

接下来,使用Tee命令行开关:
get-childitem "$path" -recurse -directory |   select -expand fullname | tee -file  "$file"

关于powershell - Powershell-输出目录递归到控制台和文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25565087/

相关文章:

powershell - ffmpeg - 转换文件但保留与原始文件相同的修改日期?

powershell - 我可以在Powershell中自定义 'not recognized as the name of a cmdlet'错误吗?

powershell - 如何使用PowerShell从应用程序中过滤字符流?

.net - 如何使用 .NET 确定 Windows 上网络接口(interface)的绑定(bind)顺序?

security - 如何使用PowerShell获取进程的启动帐户?

mysql - 结果集是 1 条记录的对象,多条记录的数组?

caching - 使AppFabric缓存退出限制模式

powershell - 如何使用 Powershell 2.0 版解压缩 Zip 文件?

mysql - MySQL备份到多个文件的脚本

powershell - Azure-SSMS-PowerShell