Introduction
When trying to install the Power Shell module using install-module, I have encountered the below issue
In the above scenario, I am trying to install module ‘PSLogging’, (this is used to write the logs to the local and network folders by using the readily built-in functions) and it has thrown me the error. It gives the below message,
PackageManagement\Install-Package: No match was found for the specified search criteria and module name ‘PS Logging. Try get-PSRepository to see all the registered module repositories.
When trying to run the command Get-PSRepository, it is giving the message ‘Unable to find module repositories’.
Background
Before going into the error details, let's try to understand what Install-Module does. The cmdlet Install-Module gets one or more modules based on the specified criteria from Microsoft PowerShell online repository https://www.powershellgallery.com/. All the final versions of the modules are carefully inspected and are published in Microsoft PowerShell Online repositories by MSFT. These modules are developed by Internal MSFT team and from the third parties and individual contributors.
In some organizational environments, the network traffic is restricted and these are tunneled through secure proxies. It is required to authenticate to a proxy server before running the install-module. The other reasons could be latest TLS secure channel might not have been enabled. You can find more about the TLS in the references section.
Method 1: Enabling latest TLS secure channel
To enable the TLS1.2 (which is the latest version while writing this article) in PS scripts, you need to add the below line at the very start
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Method 2: Authenticating to Web Proxy
In some cases, you might have to exclusively authenticate to proxy server using your network credentials. Using PowerShell you can run the below line of commands to authenticate to proxy server.
Step 1: Get the default proxy
Get the default proxy that is defined at your system configuration level by running the below command.
[System.Net.WebRequest]::DefaultWebProxy = [System.Net.WebRequest]::GetSystemWebProxy()
In some cases, it may be needed to authenticate to proxy server by defining the URL. In such cases you can use the below syntax.
[System.Net.WebRequest]::DefaultWebProxy = New-Object System.Net.WebProxy("http://WEBPROXYURL:PORT",$true)
Example:
[System.Net.WebRequest]::DefaultWebProxy = New-Object System.Net.WebProxy("http://webproxy.contoso.com:9191",$true)
Step 2: Authenticate to proxy by using your default network credentials
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
Step 3: Bypass on local traffic
This step is optional but recommended to number of times the requests within local network are getting authenticated. By running the below command, you are instructing the script to bypass authentication on local.
[system.net.webrequest]::defaultwebproxy.BypassProxyOnLocal = $true
Complete Script for dedicated proxy server:
#Update the webproxy url with your company proxy web server url and port no.
[System.Net.WebRequest]::DefaultWebProxy = New-Object System.Net.WebProxy("http://webproxy.contoso.com:9191",$true)
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
[system.net.webrequest]::defaultwebproxy.BypassProxyOnLocal = $true
Complete Script for default proxy server:
[System.Net.WebRequest]::DefaultWebProxy = [System.Net.WebRequest]::GetSystemWebProxy()
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
[system.net.webrequest]::defaultwebproxy.BypassProxyOnLocal = $true
PS Module Path
On successful module installation from the Microsoft PowerShell gallery the modules are available at the default location which is C:\Program Files (x86)\WindowsPowerShell\Modules
You can also validate the module path by going to Start - ‘Edit the system environment variables’ - PSModulePath
Conclusion
Thus, in this article, we have seen the issue with PowerShell install module command and how to fix it by using different methods.
References