The 'Get-PSRepository' command was found in the module 'PowerShellGet', but the module could not be loaded.
chrishuynhc opened this issue · 3 comments
After installing Powershell 7.1 and running a script that calls Get-PSRepository, the following error is displayed:
Get-PSRepository : The 'Get-PSRepository' command was found in the module 'PowerShellGet', but the module could not be
loaded. For more information, run 'Import-Module PowerShellGet'.
At C:\<path>\<script>:625 char:21
+ $psRepository = Get-PSRepository | ForEach-Object { $_.Name }
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-PSRepository:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
PackageManagement\Get-PackageProvider : The term 'PackageManagement\Get-PackageProvider' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
At C:\program files\powershell\7\Modules\PowerShellGet\PSModule.psm1:2934 char:26
+ ... $nugetProvider = PackageManagement\Get-PackageProvider -ErrorA ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (PackageManagement\Get-PackageProvider:String) [], CommandNotFoundExcept
ion
+ FullyQualifiedErrorId : CommandNotFoundException
PackageManagement\Get-PackageProvider : The term 'PackageManagement\Get-PackageProvider' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
At C:\program files\powershell\7\Modules\PowerShellGet\PSModule.psm1:2948 char:40
+ ... ailableNugetProviders = PackageManagement\Get-PackageProvider -Name $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (PackageManagement\Get-PackageProvider:String) [], CommandNotFoundExcept
ion
+ FullyQualifiedErrorId : CommandNotFoundException
I have seen this repro for multiple users, all running or having recently installed Powershell 7. However, using Powershell 5, the issue is not reprod. I checked that the PSModule path was valid and that both the PowershellGet and PackageManagement modules existed and were being resolved correctly. I also tried setting the ExecutionPolicy to Unrestricted, but am still getting the same output. Trying to run 'Import-Module PowerShellGet' or updating was also not successful.
Checking that the command exists:
PS:21 [C:\] > Get-Command PackageManagement\Get-PackageProvider
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Get-PackageProvider 1.4.7 PackageManagement
Has anyone experienced this issue before? Please let me know if there is any extra context I can provide.
Thanks @chrishuynhc would it be possible to run gmo PackageManagement, PowerShellGet -ListAvailable in this session context, we are particularly interested in not only the versioning (which appears to be 1.4.7 for PackageManagement) but also the path to PackageMangement and PowerShellGet (the error implies they may not be accessible for some reason).... Also this looks like Window but it would be great to get any additional information on the OS/Platform/Environment...in PowerShell 6+ the modules path is different from the PowerShell 5.1 module path, there is nothing in your script that could possibly be deleting or removing the modules folder? Thanks!
@SydneyhSmith Sure thing! This is the result:
PS:49 [C:\] > gmo PackageManagement, PowerShellGet -ListAvailable
Directory: C:\program files\powershell\7\Modules
ModuleType Version PreRelease Name PSEdition ExportedCommands
---------- ------- ---------- ---- --------- ----------------
Script 1.4.7 PackageManagement Desk {Find-Package, Get-Package, Get-PackageProvider, Get-PackageSource…}
Script 2.2.5 PowerShellGet Desk {Find-Command, Find-DSCResource, Find-Module, Find-RoleCapability…}
Directory: C:\Program Files\WindowsPowerShell\Modules
ModuleType Version PreRelease Name PSEdition ExportedCommands
---------- ------- ---------- ---- --------- ----------------
Script 1.0.0.1 PowerShellGet Desk {Install-Module, Find-Module, Save-Module, Update-Module…}
As far as I know, we don't modify the modules folder, but I can double check and get back to you. And yes I am running Windows 10 (x64) with the following PowerShell version:
PS:50 [C:\] > $host.Version
Major Minor Build Revision
----- ----- ----- --------
7 1 0 -1
After looking through the code again, I found that one of our commands calls PowerShell.exe to launch the above script. The problem then would occur if a user had launched the command from inside a pwsh.exe window (PowerShell 7). As a result, changing the command's call from PowerShell.exe to pwsh.exe fixed the issue! I am not sure what the specifics are, but I assume that launching PowerShell 7 from within a PowerShell 5 window messed up how modules are resolved from the PSModule paths.