The term ‘Install-Module’ is not recognized as the name of a cmdlet.
Have you ever found yourself in trouble because your Powershell is not working anymore after a colleague told you to just delete every module of Windows Powershell? No problem… you can just use Install-Module to reinstall everything. Oops… Install-Module is part of the module you just deleted. Here is the guide on how to escape this chicken and egg problem.
How to restore Install-Module manually
To restore functionality, you need two modules. PowerShellGet and its dependency PackageManagement. You can get them from the Powershell Gallery. Search for the modules and click on ‘Manual Download’. There you will also find a helpful description of how to install these Nuget packages manually. Follow the instructions step by step. Only in step 5, you should first create a subfolder containing the version of the module. Restart your PowerShell and you can install all other modules now.
Are you a developer and too lazy to do things manually?
Since you already use Powershell, why not just use Powershell?
# provide the path to the folder where the NuGet package should be extracted | |
# use a full path, e.g. "C:\MySubfolder\MyFolder" | |
$destinationFolder = "C:\MySubfolder\MyFolder" | |
# Fill this variable with the version from the powershell gallery you want to extract | |
$PowerShellGetVersion = "2.2.5" | |
$PackageManagementVersion = "1.4.8.1" | |
# Define the NuGet package name | |
$PowerShellGet = "PowerShellGet" | |
$PackageManagement = "PackageManagement" | |
function ExtractAndCleanNugetPackage { | |
param( | |
[string]$destinationFolder, | |
[string]$nugetPackageName, | |
[string]$version | |
) | |
$extractPath = "$destinationFolder\$nugetPackageName" | |
$installationPath = "$extractPath\$version" | |
if (!(Test-Path -Path $installationPath)) { | |
New-Item -ItemType Directory -Force -Path $installationPath | |
} | |
# Download the NuGet package | |
$nugetUrl = "https://www.powershellgallery.com/api/v2/package/$nugetPackageName/$version" | |
Invoke-WebRequest -Uri $nugetUrl -OutFile "$extractPath\$nugetPackageName.nupkg" | |
# Unblock the downloaded file, rename it to .zip because Expand-Archive does not support .nupkg files and extract it | |
Unblock-File -Path "$extractPath\$nugetPackageName.nupkg" | |
Rename-Item -Path "$extractPath\$nugetPackageName.nupkg" -NewName "$extractPath\$nugetPackageName.zip" | |
Expand-Archive -Path "$extractPath\$nugetPackageName.zip" -DestinationPath $installationPath -Force | |
# Delete nuget specific files and the downloaded zip file | |
$folderToDelete = "_rels" | |
$folderToDelete2 = "package" | |
$fileToDelete = "$nugetPackageName.nuspec" | |
$fileToDelete2 = "`[Content_Types`].xml" | |
Remove-Item -Path "$installationPath\$folderToDelete" -Recurse | |
Remove-Item -Path "$installationPath\$folderToDelete2" -Recurse | |
Remove-Item -Path "$installationPath\$fileToDelete" -Force | |
Remove-Item -LiteralPath "$installationPath\$fileToDelete2" -Force | |
Remove-Item -Path "$extractPath\$nugetPackageName.zip" -Force | |
# Unblock all files in the extracted folder to avoid security warnings | |
Get-ChildItem -Path $installationPath -Recurse | Unblock-File | |
} | |
ExtractAndCleanNugetPackage -destinationFolder $destinationFolder -nugetPackageName $PowerShellGet -version $PowerShellGetVersion | |
ExtractAndCleanNugetPackage -destinationFolder $destinationFolder -nugetPackageName $PackageManagement -version $PackageManagementVersion |
Finally, copy the two folders PowerShellGet and PackageManagement to your Powershell Module folder. You will get a warning that the files are marked as untrusted. If anyone finds a solution, please leave a comment.
If you are simply smart and pragmatic
Use the Windows Recycle Bin to restore the PowerShellGet and PackageManagement modules.
Please also leave a comment if anyone else is experiencing the same problem. Together we are strong 💪♥️❤♥️
Foto von Tim Marshall auf Unsplash
Top comments (0)