PowerShell-Profile/functions/Get-ObjectCount.ps1
2024-05-01 14:12:54 +02:00

31 lines
898 B
PowerShell

function Get-ObjectCount {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$Path,
[switch]$Recurse,
[switch]$IncludeSize
)
$objects = $null
if ($Recurse) {
$objects = Get-ChildItem -Path $Path -Recurse
} else {
$objects = Get-ChildItem -Path $Path
}
$count = $objects.Count
if ($IncludeSize) {
$totalSizeBytes = ($objects | Measure-Object -Property Length -Sum).Sum
$totalSizeKB = [math]::Round($totalSizeBytes / 1KB, 2)
$totalSizeMB = [math]::Round($totalSizeBytes / 1MB, 2)
$totalSizeGB = [math]::Round($totalSizeBytes / 1GB, 2)
Write-Output "Total objects: $count, Total size: $totalSizeBytes bytes ($totalSizeKB KB, $totalSizeMB MB, $totalSizeGB GB)"
} else {
Write-Output "Total objects: $count"
}
}