Public/Get-DiskSpaceReport.ps1

<#
.SYNOPSIS
    Retrieves detailed disk space utilization for a specified UNC path, providing information about total size, free space, used space, and the percentage of free space.
     
.DESCRIPTION
    This function is designed to support inputs via the pipeline and can be used to generate reports for multiple network shares in a batch.
 
.PARAMETER UNC
    The UNC path of the network share to check.
 
.PARAMETER Total
    The total size of the disk in a human-readable format (e.g., "500GB", "1TB").
 
.EXAMPLE
    Get-DiskSpaceReport -UNC "\\server\share" -Total "500GB"
    Retrieves totalsize, free space, used space , percentage free space for the UNC path.
 
.NOTES
    It can be run as a normal user but make sure atleast you have read permissions on the UNC Path
#>

Function Get-DiskSpaceReport {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string] $UNC,

        [Parameter(Mandatory = $true)]
        [string] $Total
    )

    Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;
 
public static class DiskUtil {
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern bool GetDiskFreeSpaceEx(
        string lpDirectoryName,
        out ulong lpFreeBytesAvailable,
        out ulong lpTotalNumberOfBytes,
        out ulong lpTotalNumberOfFreeBytes);
}
'@
 -ErrorAction SilentlyContinue

    # Converts size format to bytes
    function ConvertTo-Bytes {
        Param ([string]$Size)
        $units = @{KB=1KB; MB=1MB; GB=1GB; TB=1TB}
        if ($Size -match '^(?<number>\d+(\.\d+)?)\s*(?<unit>KB|MB|GB|TB)$') {
            $number = [double]::Parse($matches['number'], [Globalization.CultureInfo]::InvariantCulture)
            $unit = $matches['unit']
            return [uint64]($number * $units[$unit])
        }
        throw "Invalid size format: $Size"
    }

    # Formats bytes into human-readable sizes
    function Format-Size {
        Param ([uint64]$Bytes)
        $units = @("Bytes", "KB", "MB", "GB", "TB")
        $size = $Bytes
        $unitIndex = 0
        while ($size -ge 1024 -and $unitIndex -lt $units.Length) {
            $size /= 1024
            $unitIndex++
        }
        return ("{0:N2} {1}" -f $size, $units[$unitIndex])
    }

    # Main logic to retrieve disk space details
    $userFreeSpace, $totalSize, $totalFreeSpace = 0, 0, 0
    if ([DiskUtil]::GetDiskFreeSpaceEx($UNC, [ref]$userFreeSpace, [ref]$totalSize, [ref]$totalFreeSpace)) {
        $totalSizeInBytes = ConvertTo-Bytes -Size $Total
        $usedSpaceInBytes = $totalSizeInBytes - $totalFreeSpace
        [pscustomobject]@{
            UNC = $UNC
            'Total Size' = $Total
            'Free Space' = Format-Size -Bytes $totalFreeSpace
            'Used Space' = Format-Size -Bytes $usedSpaceInBytes
            'Percentage Free Space' = ("{0:N2} %" -f [math]::Round(($totalFreeSpace / $totalSizeInBytes) * 100, 2))
        }
    } else {
        $LastError = [ComponentModel.Win32Exception][Runtime.InteropServices.Marshal]::GetLastWin32Error()
        Write-Error "Failed to get disk space details for $UNC : $LastError"
    }
}