59 lines
1.4 KiB
PowerShell
59 lines
1.4 KiB
PowerShell
|
function Get-FolderSizeMb {
|
||
|
<#
|
||
|
.SYNOPSIS
|
||
|
Retrieves the size in MB of the specified folder.
|
||
|
|
||
|
.PARAMETER Path
|
||
|
The path to evaluate.
|
||
|
|
||
|
.PARAMETER ComputerName
|
||
|
The name of the server where the operation should be performed. If omitted, defaults to the current host.
|
||
|
|
||
|
.OUTPUTS
|
||
|
The size of the specified folder in MB.
|
||
|
|
||
|
.EXAMPLE
|
||
|
Get-FolderSizeMb -Path 'C:\Temp'
|
||
|
|
||
|
129.49
|
||
|
|
||
|
.EXAMPLE
|
||
|
Get-FolderSizeMb -Path 'C:\This\Path\Does\Not\Exist'
|
||
|
|
||
|
0
|
||
|
#>
|
||
|
[CmdLetBinding()]
|
||
|
[OutputType([decimal])]
|
||
|
param(
|
||
|
[Parameter(Mandatory = $true)]
|
||
|
[ValidateNotNullOrEmpty()]
|
||
|
[string] $Path,
|
||
|
|
||
|
[Parameter(Mandatory = $false)]
|
||
|
[string] $ComputerName = $null
|
||
|
)
|
||
|
|
||
|
$scriptBlock = {
|
||
|
param($p)
|
||
|
|
||
|
if ( Test-Path $p -PathType Container ) {
|
||
|
|
||
|
return [System.Math]::Round( ((Get-ChildItem -Path $p -Recurse -ErrorAction SilentlyContinue -Force) | Measure-Object -Property Length -Sum).Sum / 1Mb, 2)
|
||
|
|
||
|
} else {
|
||
|
|
||
|
return 0
|
||
|
}
|
||
|
}
|
||
|
|
||
|
# Determine if we are executing remotely.
|
||
|
$splatParams = @{}
|
||
|
if ( -not (Test-StringIsNullOrWhitespace -Value $ComputerName) -and -not (Compare-StringToLocalMachineIdentifiers -stringToCheck $ComputerName )) {
|
||
|
|
||
|
$splatParams['ComputerName'] = "$ComputerName"
|
||
|
}
|
||
|
|
||
|
$result = Invoke-Command -ScriptBlock $scriptBlock -ArgumentList $Path @splatParams
|
||
|
return $result
|
||
|
}
|