Source/Public/MCT-GetFolderInfo.ps1

Function MCT-GetFolderInfo {

    <#
    .SYNOPSIS
        Gets the number of files and total storage for a specified folder.
 
    .DESCRIPTION
        Gets the number of files and total storage for a specified folder on the local or remote computer.
        If no computer is specified, the local computer is queried
 
    .EXAMPLE
        'SVR1', 'SVR2' | GetFolderInfo -path 'C:\Data'
 
        Folder info is retrieved from 2 computers
            Names are presented through the pipeline
 
    .EXAMPLE
        GetFolderInfo -path 'C:\data' -recurse -computerName 'SVR1', 'SVR2'
 
        Folder info is retrieved from 2 computers as presented via the ComputerName parameter
            -Recurse directs the function to process underlying subdirectories
 
 
    .NOTES
        Version 2022-04-06
        Author Frits van Drie (3-link.nl)
 
    #>


    [CmdletBinding()]
    
    param(
        [parameter(mandatory=$false)]
        $path = "$env:USERPROFILE\documents",

        [parameter(mandatory=$false)]
        [switch]$recurse = $false,

        [parameter(mandatory=$false,ValueFromPipeline=$true)]
        [string[]]$computerName = 'localhost'

    )

    Begin {
        Write-Verbose "Start Function"

        if ( $PSBoundParameters.Keys -contains 'Verbose' ) {
            $verboseAction = 'Continue'
        }
        else {
            $verboseAction = $VerbosePreference
        }

    }

    Process {

        foreach ($deviceName in $computerName) {

            Write-Verbose "Connecting to $deviceName"

            $objReturn = Invoke-Command -ComputerName $deviceName {

                $VerbosePreference = $using:verboseAction

                $path    = $using:path
                $recurse = $using:recurse


                Write-Verbose "Connected to $env:COMPUTERNAME"
                Write-Verbose "Path : $path"
                Write-Verbose "Recurse: $recurse"


                if ( -not (Test-Path $path) ) {
                    Write-Error "$path is not a valid path"
                    break
                }
                    Write-Verbose "$path exists" 

                if ( -not ((Get-Item $path).psIsContainer) ){
                    Write-Warning "$path is not a valid folder" 
                    break
                }
                    Write-Verbose "$path is a container"


                # Collect Files
                try {
                    Write-Verbose "Retrieving files in $path"
                    $fileList = Get-ChildItem -Path $path -Recurse:$recurse -File -ErrorAction stop
                    Write-Verbose "Success"
                    Write-Verbose "Found $($fileList.count) files"
                    Write-Verbose "Measuring files"
                    $objReturn = $fileList | Measure-Object -Property Length -Sum
                }
                catch {
                    Write-Warning "$path is not a valid path"
                    break
                }

                return $objReturn

            } # end invoke

            Write-Output $objReturn

        }  # end Foreach

        

    } # end Process

    End {
        Write-Verbose "End Function"
    }


}