powershell - Get-ChildItem文件/文件夹

标签 powershell

我正在尝试让Get-ChildItem检索文件夹的内容,并显示文件夹中每个对象的大小,对象的名称和类型(文件/目录)。但是,我很难让只有这三列才能工作。

名称和长度很简单,但是如何显示文件和文件夹之间的差异?

最佳答案

检查PSIsContainer属性。文件夹设置为$ true,文件设置为$ false。这是一个例子:

Get-ChildItem | Foreach-Object{
    New-Object PSObject -Property @{
        Name = $_.Name
        SizeMB = if($_.PSIsContainer) { (Get-ChildItem $_.FullName -Recurse | Measure-Object Length -Sum).Sum/1mb  } else {$_.Length}
        Type = if($_.PSIsContainer) {'Directory'} else {'File'}
    }
}

关于powershell - Get-ChildItem文件/文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14030871/

相关文章:

c# - 在WiX安装中从C#自定义操作调用PowerShell命令

powershell - 在 DSC 中使用文件提供程序 - 确保目标仅包含来自源的文件

c# - PowerShell 使用现有 session 状态填充 InitialSessionState

Powershell删除特定的注册表项

Powershell:如何阻止脚本中显示错误?

arrays - 如何在PowerShell中向数组动态添加元素?

PowerShell 文件存在性检查返回假阴性

powershell - Powershell sqlserver上下文中的复制项目失败

powershell - Copy-Item -Recurse 跳过创建一个子目录

xml - 哈希表包含$ null值