Commands/Close-OpenPackage.ps1

function Lock-OpenPackage
{
    <#
    .SYNOPSIS
        Closes an Open Package
    .DESCRIPTION
        Closes an Open Package.
    .NOTES
        This will free the package from memory
    .EXAMPLE
        # Import OP, make it a package, and lock it
        Import-Module OP -PassThru |
            Get-OpenPackage |
            Close-OpenPackage
    .EXAMPLE
        # Import OP, make it a package, and lock it
        impo OP -PassThru | op | Close-OpenPackage
    #>

    [CmdletBinding(ConfirmImpact='Medium')]
    [Alias('Close-OP', 'csop','csOpenPackage')]
    param(
    # The input object. This should be a package.
    [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
    [Alias('Package')]
    [PSObject]
    $InputObject        
    )

    process {
        # If the input is not a package
        if ($InputObject -isnot [IO.Packaging.Package]) {
            if ($inputObject.Package -is [IO.Packaging.Package]) {
                $inputObject = $inputObject.Package
            } else {
                return $InputObject # pass it thru.
            }
            
        }

        $InputObject.Close() # this also makes the current package useless

        if ($inputObject.MemoryStream -is [IO.MemoryStream]) {
            $InputObject.MemoryStream.Close()
            $InputObject.MemoryStream.Dispose()
        }
    }
}