PowerShell / PowerShell/PSResourceGet

Add `-Path` to `Uninstall-PSResource`

Open
#1,453 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Area-Uninstall feature_request
Dominant language
C#
Stars
576
Forks
114
Avg merge
1d 2h
Merged PRs (30d)
7

Description

Summary of the new feature / enhancement

It'd be nice if Uninstall-PSResource could uninstall a given module by -Path, especially now that Microsoft.PowerShell.PSResourceGet still uses hardcoded paths to module install directories given scope, vs. respecting PSModulePath or having some setting for what directory to work against.

Example usage that should work:

Click to expand
# Assets
$ModuleToInstall = [string] 'Az.Accounts'
$ModuleInstallDirectoryParent = [string] [System.IO.Path]::Combine(
    [System.Environment]::GetFolderPath('Desktop'),
    [datetime]::Now.ToString('yyyyMMddHHmmss')
)
$ModuleInstallDirectory = [string] [System.IO.Path]::Combine(
    $ModuleInstallDirectoryParent,
    'Microsoft',
    'PowerShell',
    'Modules'
)


# Create $ModuleInstallDirectory
if (-not [System.IO.Directory]::Exists($ModuleInstallDirectory)) {
    $null = [System.IO.Directory]::CreateDirectory($ModuleInstallDirectory)
}


# Add $ModuleInstallDirectory to PSModulePath process scope if not already there, so Get-Module can find it
if ([System.Environment]::GetEnvironmentVariable('PSModulePath','Process').Split([System.IO.Path]::PathSeparator) -notcontains $ModuleInstallDirectory) {
    [System.Environment]::SetEnvironmentVariable(
        'PSModulePath',
        ([System.Environment]::GetEnvironmentVariable('PSModulePath','Process'), $ModuleInstallDirectory -join [System.IO.Path]::PathSeparator -as [string]),
        'Process'
    )
}


# Import PSResourceGet
$null = Import-Module -Name 'Microsoft.PowerShell.PSResourceGet'


# Get latest version of $ModuleToInstall from PowerShell Gallery
$ModuleInfo = Find-PSResource -Type 'Module' -Repository 'PSGallery' -Name $ModuleToInstall


# Install $ModuleToInstall to $ModuleInstallDirectory
$null = Save-PSResource -Repository 'PSGallery' -TrustRepository -IncludeXml -Path $ModuleInstallDirectory -Name 'Az.Accounts'


# Get module from $ModuleInstallDirectory
## With Microsoft.PowerShell.Core - Works because we added $ModuleInstallDirectory to PSModulePath
Microsoft.PowerShell.Core\Get-Module -Name $ModuleToInstall -ListAvailable | Where-Object -FilterScript {$_.'Path'.StartsWith($ModuleInstallDirectory)}

## With Microsoft.PowerShell.PSResourceGet - Works with "-Path", nice
Get-InstalledPSResource -Path $ModuleInstallDirectory -Name $ModuleToInstall


# Try to uninstall it using Microsoft.PowerShell.PSResourceGet
## Uninstall-PSResource -Path - Returns error "Uninstall-PSResource: A parameter cannot be found that matches parameter name 'Path'"
Uninstall-PSResource -Path $ModuleInstallDirectory -Name $ModuleToInstall -Version $ModuleInfo.'Version'

## Pipe module into Uninstall-PSResource - Returns error "Uninstall-PSResource: Cannot uninstall resource 'Az.Accounts' because it does not exist"
Get-InstalledPSResource -Path $ModuleInstallDirectory -Name $ModuleToInstall -Version $ModuleInfo.'Version' | Uninstall-PSResource


# Clean up
$null = [System.IO.Directory]::Delete($ModuleInstallDirectoryParent,$true)
Proposed technical implementation details (optional)

Add -Path to Uninstall-PSResource.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Uninstall-PSResource entry point and compare it with the path-aware Get-InstalledPSResource behavior shown in the example. Check how -Name and -Version are handled, then verify that Uninstall-PSResource accepts -Path and removes the resource from the supplied installation directory.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, powershell
Domain
cli
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.