Exploring the PowerShell Gallery
Grabbing and organizing cmdlets has been made much easier in PowerShell V5.
Ever have the need to get a PowerShell module but hated having to search for it online on a variety of sites and then having to download the module to throw it into your Modules folder? Ok, it may not be that bad of a thing, but that the process of getting a module installed is about to change. Starting with PowerShell V5, we now have a new module called PowerShellGet which comes with 11 cmdlets that you can use to explore, download, install and even publish your own modules to the gallery.
You can go to the PowerShell Gallery Web site and view all of the available modules, but if you click on one you will not have the option to download the module. Instead you are shown the command needed to install the module from PowerShell using Install-Module. Also listed is the command to save the module so you can be sure that it doesn't do anything malicious as well using Save-Module.
Starting from the top, we should search for a module and then download it so we can be sure that it meets our needs and doesn't potentially have code that would be considered malicious before we move ahead and install it. Note that if you just type Find-Module and hit return, you will be flooded with all of the modules that reside on the gallery. There are quite a few ways to filter for results with this cmdlet.
I am going to use the –Name parameter and filter for a module that sounds good to me. I'm not too concerned about a specific version or tags and I am not interested in a DSCResource at this time.
Find-Module -Name Posh* | Out-GridView -PassThru
Notice how I piped the output of the results into a gridview UI using Out-GridView. Using the –PassThru parameter means that if I select something and click OK, it will present the object at the end that could then be piped into another command, such as Save/Install-Module to receive the module. If there are a lot of items returned, I can easily use the Filter text box to quickly locate what I need. In this case, I am going to download my own module, PoshRSJob and check it out.
Find-Module -Name Posh* | Out-GridView -PassThru |
Save-Module -Path C:\temp -Verbose
Because I do not actually trust this location, I am prompted with this asking if I want to go ahead with the downloading of this module. I'll say yes and continue on with the download.
Perfect! Now I can go in and make sure nothing looks out of the ordinary. Of course, I could simply copy the folder and its items to my modules directory, but instead I will go ahead and run Install-Module instead to download the files and place them in the appropriate location. Because I want to show off another cmdlet soon, I am actually going to install an older version of the module.
Find-Module poshrsjob -AllVersions | Out-GridView -PassThru |
One version back should be good enough this time around.
And now I have a module to use without having to download from a website and manually placing the files in my modules folder. If I wanted to verify this I can run Get-Module –ListAvailable –Name PoshRSJob to see that the module is in fact available and is the proper version that I chose to install.
To see all of the installed modules from the gallery, I can use Get-InstalledModule.
This is useful to know what modules came from the PowerShellGallery (or another internal gallery). We are also able to see the version and description of the modules that were downloaded and installed.
If I am done with a module and no longer need in installed, I can just use Uninstall-Module to completely remove it from my system.
Uninstall-Module -Name Reflection -Verbose
Maybe I want to check to see if a module needs to be updated. I can use Update-Module to check and update a module if the version is higher than what is already installed on my system.
Update-Module -Name PoshRSJob -Verbose
Now I have the latest update to my module! If I already have the latest module version, then nothing happens at all.
No big deal at all, right? Just a simple check and no need to re-download the module since the version is already up to date or higher than what is available on the gallery.
One last thing in this article: you noticed how I didn't trust the repository as I was prompted for confirmation before downloading the module? You can change this by updating the InstallationPolicy for a particular repository. In this case, we only have one repository as show when we use Get-PSRepository.
We can change this using Set-PSRepository and setting the InstallationPolicy to Trusted.
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted -Verbose
Now you can download/install modules without the confirmation prompt. Keep in mind that this shouldn't be used against repositories that you do not trust as it provides a little more time to hold off on installation a potentially bad module.
There are still some other cool things with the PowerShellGet module such as publishing your own module and adding another PSRepository but that is a topic for another article. Now you can go forward and explore the gallery check out some of the cool modules that the community has published!
Boe Prox is a Microsoft MVP in Windows PowerShell and a Senior Windows System Administrator. He has worked in the IT field since 2003, and he supports a variety of different platforms. He is a contributing author in PowerShell Deep Dives with chapters about WSUS and TCP communication. He is a moderator on the Hey, Scripting Guy! forum, and he has been a judge for the Scripting Games. He has presented talks on the topics of WSUS and PowerShell as well as runspaces to PowerShell user groups. He is an Honorary Scripting Guy, and he has submitted a number of posts as a to Microsoft's Hey, Scripting Guy! He also has a number of open source projects available on Codeplex and GitHub. His personal blog is at http://learn-powershell.net.