【问题标题】:How to save powershell script results to a file如何将powershell脚本结果保存到文件中
【发布时间】:2016-04-20 21:53:50
【问题描述】:

我正在使用下面的脚本来显示所有子文件夹的名称和这些文件夹中的文件数。我不知道如何将结果保存到文件中。我已尽我所能,并在网上搜索了几个小时。

dir -recurse |  ?{ $_.PSIsContainer } | %{ Write-Host $_.FullName (dir $_.FullName | Measure-Object).Count }

【问题讨论】:

    标签: file powershell count directory


    【解决方案1】:

    不要使用Write-Host。该 cmdlet 仅写入控制台(即,您不能将其重定向到文件)。

    改用这样的东西:

    Get-ChildItem -Recurse | Where-Object { $_.PSIsContainer } | ForEach-Object {
      $_.FullName
    }
    

    【讨论】:

    • 很抱歉,我是 PS 新手,可以看到您没有放置整个脚本,因为它没有要计算的部分,当我运行它时,它只会给我文件夹列表,而不是每个文件夹中的文件数。你能把我需要运行的整个脚本来计算吗?谢谢
    【解决方案2】:

    找到了另一种让我保存到文件的方法:

    ls -rec | ? {$_.mode -match 'd'} | select FullName,  @{N='Count';E={(ls $_.FullName | measure).Count}} | Out-File C:\results.txt
    

    【讨论】:

      【解决方案3】:

      我拿走了你的代码并升级了它。试试这个:

      $OUserOut = @()
      Get-ChildItem -Recurse | Where-Object { $_.PSIsContainer } | ForEach-Object {
        $ObjProperties = New-Object PSObject
        Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name FullName -Value $_.FullName
        Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name InternalObj -Value (dir $_.FullName | Measure-Object).Count
        $OUserOut += $ObjProperties
      }
      $OUserOut | Out-GridView -Title "Objects"
      $OUserOut |  Export-Csv $env:USERPROFILE\Desktop\Out.csv   
      

      最后两行包含输出文件的信息。 “Out-GridView”是一个表格,用于显示信息,另一行用于将结果导出到 csv 文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-06
        • 1970-01-01
        • 1970-01-01
        • 2020-02-15
        • 1970-01-01
        • 1970-01-01
        • 2018-04-01
        • 1970-01-01
        相关资源
        最近更新 更多