Skip to content

Instantly share code, notes, and snippets.

@shiguruikai
Last active July 5, 2020 13:35
Show Gist options
  • Save shiguruikai/f5524e6ffa5ef947f9ee802abb81ca18 to your computer and use it in GitHub Desktop.
Save shiguruikai/f5524e6ffa5ef947f9ee802abb81ca18 to your computer and use it in GitHub Desktop.
[OutputType([System.IO.FileSystemInfo])] # +Property(Bytes, HumanBytes)
[CmdletBinding(
DefaultParameterSetName = "Path"
)]
param (
[Parameter(
Position = 0,
ValueFromPipeline,
ParameterSetName = "FileSystemInfo")]
[ValidateNotNullOrEmpty()]
[System.IO.FileSystemInfo[]] $FileSystemInfo,
[Parameter(
Position = 0,
ValueFromPipeline,
ParameterSetName = "Path",
ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string[]] $Path = (Get-Location).Path,
[Parameter(
Position = 0,
ParameterSetName = "LiteralPath",
ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string[]] $LiteralPath,
[Parameter()]
[Alias("d")]
[ValidateNotNullOrEmpty()]
[int] $Depth = 0,
[Parameter()]
[switch] $Force
)
function Format-Bytes {
[OutputType([string])]
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory, ValueFromPipeline)]
[long] $Value,
[Parameter(Position = 1)]
[int] $DecimalPlaces = 1
)
if ($DecimalPlaces -lt 0) {
$DecimalPlaces = 0
}
if ($Value -lt 0) {
return $('-' + (Format-Bytes [System.Math]::Abs($Value) $DecimalPlaces))
}
return $(switch ($Value) {
{ $_ -lt 1KB } { "{0:n$DecimalPlaces} B" -f $Value; break }
{ $_ -lt 1MB } { "{0:n$DecimalPlaces} KB" -f ($Value / 1KB); break }
{ $_ -lt 1GB } { "{0:n$DecimalPlaces} MB" -f ($Value / 1MB); break }
{ $_ -lt 1TB } { "{0:n$DecimalPlaces} GB" -f ($Value / 1GB); break }
{ $_ -lt 1PB } { "{0:n$DecimalPlaces} TB" -f ($Value / 1TB); break }
default { "{0:n$DecimalPlaces} PB" -f ($Value / 1PB) }
})
}
function Set-DefaultDisplayPropertySet(
[Parameter(Mandatory, ValueFromPipeline)]
[Object] $InputObject,
[parameter(Mandatory, Position = 0)]
[string[]] $DefaultProperties
) {
$defaultDisplayPropertySet = New-Object Management.Automation.PSPropertySet(
'DefaultDisplayPropertySet', [string[]] $DefaultProperties
)
$PSStandardMembers = [Management.Automation.PSMemberInfo[]] @($defaultDisplayPropertySet)
$InputObject | Add-Member MemberSet PSStandardMembers $PSStandardMembers -Force -PassThru
}
$items = if ($FileSystemInfo) {
$FileSystemInfo
}
elseif ($Depth) {
if ($LiteralPath) {
Get-ChildItem -LiteralPath $LiteralPath -Depth ($Depth - 1) -Force:$Force
}
else {
Get-ChildItem -Path $Path -Depth ($Depth - 1) -Force:$Force
}
}
else {
if ($LiteralPath) {
Get-Item -LiteralPath $LiteralPath -Force:$Force
}
else {
Get-Item -Path $Path -Force:$Force
}
}
$items | ForEach-Object {
$_ | Add-Member Bytes ([long] (Get-ChildItem -LiteralPath $_.FullName -File -Recurse -Force:$Force | Measure-Object Length -Sum).Sum)
$_ | Add-Member HumanBytes (Format-Bytes $_.Bytes)
$_ | Select-Object * | Set-DefaultDisplayPropertySet Bytes, HumanBytes, Mode, FullName
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment