Open
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
.