Public/Get-LocalRepositoryPackages.ps1

function Get-LocalRepositoryPackages {
    <#
    .SYNOPSIS
        List all packages available in a local repository.
 
    .DESCRIPTION
        Scans a local repository directory and lists all available packages with their versions,
        sizes, and other metadata. Groups multiple versions of the same package.
 
    .PARAMETER Path
        Path to the local repository. Defaults to C:\LocalNuGetRepo.
 
    .PARAMETER IncludeAllVersions
        Include all versions of each package, not just the newest.
 
    .PARAMETER Format
        Output format: 'Table' (default), 'List', or 'Object'.
 
    .EXAMPLE
        Get-LocalRepositoryPackages
         
        Lists newest version of each package in default repository.
 
    .EXAMPLE
        Get-LocalRepositoryPackages -IncludeAllVersions -Format List
         
        Lists all versions of all packages in detailed list format.
 
    .EXAMPLE
        $packages = Get-LocalRepositoryPackages -Format Object
         
        Returns package information as objects for further processing.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Position = 0)]
        [ValidateScript({
            if (-not (Test-Path -Path $_ -PathType Container)) {
                throw "Path '$_' does not exist or is not a directory."
            }
            $true
        })]
        [string]$Path = $script:DefaultRepositoryPath,
        
        [Parameter()]
        [switch]$IncludeAllVersions,
        
        [Parameter()]
        [ValidateSet('Table', 'List', 'Object')]
        [string]$Format = 'Table'
    )

    try {
        Write-Verbose "Scanning repository: $Path"
        
        # Find all .nupkg files
        $nupkgFiles = Get-ChildItem -Path $Path -Filter "*.nupkg" -File
        
        if (-not $nupkgFiles) {
            Write-Host "No packages found in repository: $Path" -ForegroundColor Yellow
            return @()
        }
        
        Write-Verbose "Found $($nupkgFiles.Count) package files"
        
        # Parse package information
        $allPackages = @()
        foreach ($file in $nupkgFiles) {
            $pkgInfo = Get-NuPkgInfo -FilePath $file.FullName
            if ($pkgInfo) {
                $allPackages += [PSCustomObject]@{
                    Name = $pkgInfo.Name
                    Version = $pkgInfo.Version
                    VersionString = $pkgInfo.VersionString
                    Size = $pkgInfo.Size
                    SizeFormatted = "$($pkgInfo.Size) MB"
                    FileName = $pkgInfo.FileName
                    FilePath = $pkgInfo.FilePath
                    LastModified = $pkgInfo.LastModified
                }
            }
        }
        
        if ($allPackages.Count -eq 0) {
            Write-Host "No valid packages found (could not parse package information)" -ForegroundColor Yellow
            return @()
        }
        
        # Group and filter packages
        $packages = if ($IncludeAllVersions) {
            $allPackages | Sort-Object Name, Version -Descending
        } else {
            # Keep only newest version of each package
            $allPackages | Group-Object Name | ForEach-Object {
                $_.Group | Sort-Object Version -Descending | Select-Object -First 1
            } | Sort-Object Name
        }
        
        # Output based on format
        switch ($Format) {
            'Object' {
                return $packages
            }
            
            'List' {
                Write-Host "`nLocal Repository Packages" -ForegroundColor Green
                Write-Host "=========================" -ForegroundColor Green
                Write-Host "Repository: $Path" -ForegroundColor Gray
                Write-Host "Total Files: $($nupkgFiles.Count)" -ForegroundColor Gray
                Write-Host "Valid Packages: $($allPackages.Count)" -ForegroundColor Gray
                
                if ($IncludeAllVersions) {
                    Write-Host "Showing: All versions" -ForegroundColor Gray
                } else {
                    Write-Host "Showing: Newest versions only" -ForegroundColor Gray
                }
                
                Write-Host ""
                
                foreach ($pkg in $packages) {
                    Write-Host "Package: " -NoNewline -ForegroundColor White
                    Write-Host $pkg.Name -ForegroundColor Cyan
                    Write-Host " Version: " -NoNewline -ForegroundColor Gray
                    Write-Host $pkg.VersionString -ForegroundColor White
                    Write-Host " Size: " -NoNewline -ForegroundColor Gray
                    Write-Host $pkg.SizeFormatted -ForegroundColor White
                    Write-Host " File: " -NoNewline -ForegroundColor Gray
                    Write-Host $pkg.FileName -ForegroundColor White
                    Write-Host " Modified: " -NoNewline -ForegroundColor Gray
                    Write-Host $pkg.LastModified -ForegroundColor White
                    Write-Host ""
                }
            }
            
            'Table' {
                Write-Host "`nLocal Repository: $Path" -ForegroundColor Green
                
                if ($IncludeAllVersions) {
                    Write-Host "All Versions:" -ForegroundColor Gray
                } else {
                    Write-Host "Newest Versions:" -ForegroundColor Gray
                }
                
                $packages | Format-Table -Property @(
                    @{Name="Package"; Expression={$_.Name}; Width=25},
                    @{Name="Version"; Expression={$_.VersionString}; Width=12},
                    @{Name="Size (MB)"; Expression={$_.Size}; Width=10; Alignment="Right"},
                    @{Name="Last Modified"; Expression={$_.LastModified.ToString("yyyy-MM-dd HH:mm")}; Width=17},
                    @{Name="File Name"; Expression={$_.FileName}; Width=30}
                ) -AutoSize
                
                Write-Host "Total: $($packages.Count) package(s)" -ForegroundColor Gray
            }
        }
        
    } catch {
        Write-Error "Failed to list repository packages: $($_.Exception.Message)"
    }
}