Commands/Uninstall-OpenPackage.ps1

function Uninstall-OpenPackage
{
    <#
    .SYNOPSIS
        Uninstalls OpenPackages
    .DESCRIPTION
        Uninstalls one or more OpenPackages.
    .NOTES
        Because installed packages are just directories,
        this is a very light wrapper of Remove-Item.

        If you provide an -Identifier,
    .LINK
        Install-OpenPackage
    .LINK
        Remove-Item
    #>

    [CmdletBinding(SupportsShouldProcess,ConfirmImpact='High')]
    [Alias(
        'Uninstall-OP', 'usop', 'usOpenPackage'
    )]
    param(
    # The package identifier.
    # If this is a fully qualified directory or file name, it will be removed.
    [Parameter(Mandatory,ParameterSetName='Identifier', ValueFromPipelineByPropertyName)]
    [string[]]
    $Identifier,

    # The package version
    [Parameter(ParameterSetName='Identifier',ValueFromPipelineByPropertyName)]
    [string[]]
    $Version,

    # The direct path to any number of packages.
    # This will Remove-Item these paths
    [Parameter(ParameterSetName='PackagePath',ValueFromPipelineByPropertyName)]
    [string[]]
    $PackagePath,
    
    # The root location where packages are stored.
    # By default, this will be the locations specified in `$env:OpenPackagePath`
    [string[]]
    $PackageRoot = @(
        if ($IsLinux -or $IsMacOS) {
            $env:OpenPackagePath -split ':'
        } else {
            $env:OpenPackagePath -split ';'
        }
    )
    )

    process {
        # Get any potential paths in any package root
        $potentialPaths = @(foreach ($packageLocation in $PackageRoot) {
            # We have to have at least one identifier
            foreach ($packageId in $PSBoundParameters['Identifier']) {
                # If version was supplied
                if ($version) {                    
                    foreach ($packageVersion in $version) {
                        # look for each potential version.
                        Join-Path $packageLocation "$packageId/$packageVersion"
                    }
                } else {
                    # If no version was supplied, look for the package id.
                    Join-Path $packageLocation $packageId
                }
            }
        })
 
        # If we have provided a package path
        if ($psBoundParameters['PackagePath']) {
            # ignore any versioned paths and use that directly.
            $potentialPaths = $psBoundParameters['PackagePath']
        }


        # Check each potential path
        foreach ($potentialPath in $potentialPaths) {
            # If the path does not exist, continue
            if (-not (Test-Path $potentialPath)) { continue }             
            if ($WhatIfPreference) { # If -WhatIf was passed,
                Get-Item $potentialPath #output the item
                continue # and continue.
            }
            # If we confirmed we want to remove it
            if ($PSCmdlet.ShouldProcess("Uninstall $potentialPath")) {
                # call Remove-Item with `-Recurse` and `-Force`
                Remove-Item -Recurse -Force -Path $potentialPath
            }                        
        }                
    }
}