Functions/Remove-PythonVirtualEnvironment.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
. "$PSScriptRoot\ConfigGettersAndSetters.ps1"

function Remove-PythonVirtualEnvironment([string]$Name, [switch]$YesToAll=$false){
$Null = @(
    $virtualenvRoot = Get-VirtualEnvironmentRoot

    #Find matching virtualenv
    $virtualEnvs = Get-ChildItem $virtualenvRoot -Directory
    foreach ($environmentPath in $virtualEnvs){
        $environmentName = $environmentPath.Name
        if ($environmentName.StartsWith($Name)){
            if (!$YesToAll){
                $delete = Read-Host -Prompt "Delete virtual environment at $virtualenvRoot$($environmentName)? y/n"
                if (!$delete -or ($delete.ToLower() -ne "y")) {return}
            }
            Remove-Item -LiteralPath "$virtualenvRoot\$environmentName" -Force -Recurse
            return
        }
    }

    throw "Could not find a virtual environment in $virtualenvRoot whose name starts with $Name."
)
}