Public/Packages/Uninstall-ChocoPackage.ps1
Function Uninstall-ChocoPackage { <# .SYNOPSIS Uninstalls a chocolatey package. Doesn't asks for confirmation by default. .DESCRIPTION Uninstalls a chocolatey package. Doesn't asks for confirmation by default. Just like Chocolatey, you may need admin rights to uninstall a package. This function also accepts pipeline input. .PARAMETER Name The name of the package to uninstall. You can specify more than one package. .PARAMETER Force Will force the uninstallation of the package. .PARAMETER AskForConfirmation Ask for confirmation before uninstalling the package. .EXAMPLE Uninstall-ChocoPackage -Name vlc .EXAMPLE Uninstall-ChocoPackage -Name vlc -Force .EXAMPLE Uninstall-ChocoPackage -Name vlc -AskForConfirmation .EXAMPLE Get-ChocoPackage -Name vlc | Uninstall-ChocoPackage .OUTPUTS PSCustomObject #> [OutputType([PSCustomObject])] [CmdletBinding()] param( [Parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true)] [String[]] $Name, [Switch] $Force, [Switch] $AskForConfirmation ) begin { if ((Test-ChocoInstalled) -And (Confirm-IsAdmin)) { [String[]]$Arguments = "uninstall" if ($Force) { $Arguments += "--force" } if (-Not ($AskForConfirmation)) { $Arguments += "-y" } } } process { foreach ($package in $Name) { $CommandOutput = Invoke-ChocoCommand ($Arguments + $package) if ($CommandOutput.Status -eq "Error" -and $CommandOutput.RawOutput -like "*Cannot uninstall a non-existent package.*") { $Status = "Cannot uninstall a non-existent package" } elseif ($CommandOutput.Status -eq "Success") { if ($CommandOutput.RawOutput -like "*has been successfully uninstalled.*") { $Status = "Uninstalled" } } [PSCustomObject]@{ Name = $package Status = $Status } } } end { } } |