diff --git a/src/code/UninstallPSResource.cs b/src/code/UninstallPSResource.cs index 4cd0a4d19..6adfdfeb6 100644 --- a/src/code/UninstallPSResource.cs +++ b/src/code/UninstallPSResource.cs @@ -258,6 +258,23 @@ private bool UninstallPkgHelper(out List errRecords) } } + // If we had packages but didn't uninstall any (e.g., due to version/prerelease filters), write a warning + if (currentUninstalledDirCount == 0 && totalDirs > 0) + { + string warningMessage = Prerelease + ? $"No prerelease versions of '{String.Join(", ", Name)}' were found to uninstall." + : $"No packages matching the specified criteria for '{String.Join(", ", Name)}' were found to uninstall."; + + if (Version != null && !Version.Trim().Equals("*")) + { + warningMessage = Prerelease + ? $"No prerelease versions of '{String.Join(", ", Name)}' matching version '{Version}' were found to uninstall." + : $"No packages of '{String.Join(", ", Name)}' matching version '{Version}' were found to uninstall."; + } + + WriteWarning(warningMessage); + } + return successfullyUninstalled; } diff --git a/test/UninstallPSResourceTests/UninstallPSResource.Tests.ps1 b/test/UninstallPSResourceTests/UninstallPSResource.Tests.ps1 index 0a1fa10fc..d479843b0 100644 --- a/test/UninstallPSResourceTests/UninstallPSResource.Tests.ps1 +++ b/test/UninstallPSResourceTests/UninstallPSResource.Tests.ps1 @@ -235,6 +235,25 @@ Describe 'Test Uninstall-PSResource for Modules' -tags 'CI' { $stableVersionPkgs.Count | Should -Be 2 } + It "Write warning when using -Prerelease flag with only stable versions installed" { + # BeforeEach already installs a stable version of $testModuleName (5.0.0.0) + # Verify it's installed + $pkg = Get-InstalledPSResource $testModuleName + $pkg | Should -Not -BeNullOrEmpty + + # Try to uninstall with -Prerelease flag, should show warning + Uninstall-PSResource -Name $testModuleName -Prerelease -SkipDependencyCheck -WarningVariable warn -WarningAction SilentlyContinue + + # Module should still be present since no prerelease versions were found + $res = Get-InstalledPSResource -Name $testModuleName + $res | Should -Not -BeNullOrEmpty + $res.Name | Should -Be $testModuleName + + # Warning should have been written + $warn.Count | Should -Be 1 + $warn[0] | Should -Match "No prerelease versions" + } + It "Uninstall module using -WhatIf, should not uninstall the module" { Start-Transcript .\testUninstallWhatIf.txt Uninstall-PSResource -Name $testModuleName -WhatIf -SkipDependencyCheck