首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将PS2脚本的输出写入文本文件

将PS2脚本的输出写入文本文件
EN

Stack Overflow用户
提问于 2015-07-17 12:51:35
回答 2查看 220关注 0票数 0

我有以下脚本:

代码语言:javascript
复制
Get-ChildItem $rootPath -Recurse | 
    Where-Object {$_.PSIsContainer} | 
    Where-Object {(Get-ChildItem $_.FullName | Where-Object {!$_.PSIsContainer}|  Measure-Object | Select-Object -ExpandProperty Count) -gt 0} |
    ForEach-Object{

        $files = Get-ChildItem $_.FullName

        $props = @{
            Path = $_.FullName
            Size = "{0:N0}" -f (($files | Where-Object {!$_.PSIsContainer} |  Measure-Object -Sum Length | Select-Object -ExpandProperty Sum))
            Count = $files | Measure-Object | Select-Object -ExpandProperty Count
        }

        If ($files.Extension -match "L\d\d"){
            # These are special files and we are assuming they are alone in the directory
            # Change the path
            $props.Path = $files | Select-Object -First 1 | Select-Object -ExpandProperty FullName
        }

        New-Object -TypeName PSCustomObject -Property $props

} | Select Path,Size,Count

如何将输出写入文本文件而不是控制台?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-17 12:54:59

将其转发到Out-File。例如:

代码语言:javascript
复制
Get-ChildItem ...
<skipped>
} | Select Path,Size,Count | Out-File "files.txt"
票数 4
EN

Stack Overflow用户

发布于 2015-07-17 15:52:16

几件事。你在这里还有其他的选择值得一提。

代码语言:javascript
复制
} | Select Path,Size,Count | Set-Content "files.txt"

Set-Content也可以工作,并具有默认ACSII编码的优点。如果性能发挥作用,那么使用.Net StreamWriter无论如何都会击败Set-ContentOut-File

更重要的是,此代码旨在输出对象。使用它时,您应该使用Export-CSV来具有良好的格式化输出。

代码语言:javascript
复制
} | Select Path,Size,Count | Export-Csv -NoTypeInformation "files.csv"
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31476280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档