Exploring PowerShellGet and the PSGallery
PowerShellGet
is a module that enables you to work with external repositories. A repository is a site, either on the internet or internally, that hosts software packages. You use the cmdlets in this module to access one or more repositories that enable you to find, download, and use third-party packages from a package repository.
PowerShellGet
leverages mainly the PSGallery
repository. This repository, often referred to as a repo, is sponsored by Microsoft and contains a wealth of PowerShell functionalities, including PowerShell modules, DSC resources, PowerShell scripts, and so on. Many of the recipes in this book utilize PSGallery
resources.
To some extent, the PowerShellGet
module is similar to tools in the Linux world such as apt-get in Ubuntu or RPM in Red Hat Linux.
The PowerShellGet
module implements a number of additional *-Module
cmdlets that extend the module management cmdlets provided in the Microsoft.PowerShell.Core
module.
It's simple and easy to find and install modules from the PSGallery
. In some cases, you may wish to download the module to a separate folder. This would allow you to inspect the module, loading it manually before putting it into a folder in $env:PSModulePath
(where commands might be auto-loaded).
Getting ready
This recipe runs on the SRV1
server. The recipe also assumes you have performed the previous recipes in this chapter. In particular, you should have added the latest version of the NuGet
package provider to your system. If you have not already done so, ensure the provider is installed by performing the following:
Install-PackageProvider -Name NuGet -ForceBootstrap
How to do it...
- Review the commands available in the
PowerShellGet
module:Get-Command -Module PowerShellGet
- View the
NuGet
package provider version:Get-PackageProvider -Name NuGet | Select-Object -Property Version
- View the current version of
PowerShellGet
:Get-Module -Name PowerShellGet -ListAvailable
- Install the
PowerShellGet
module fromPSGallery
:Install-Module -Name PowerShellGet -Force
- Check the version of
PowerShellGet
:Get-Module -Name PowerShellGet -ListAvailable
- View the default
PSGallery
repositories currently available to PowerShell:Get-PSRepository
- Review the package providers in the
PSGallery
repository:Find-PackageProvider -Source PSGallery | Select-Object -Property Name, Summary | Format-Table -Wrap -autosize
- Use the
Get-Command
cmdlet to findFind-*
cmdlets in thePowerShellGet
module:Get-Command -Module PowerShellGet -Verb Find
- Get the commands in the
PowerShellGet
module:$Commands = Find-Command -Module PowerShellGet $CommandCount = $Commands.Count
- Get the modules included:
$Modules = Find-Module -Name * $ModuleCount = $Modules.Count
- Get the DSC resources available in the
PSGallery
repository:$DSCResources = Find-DSCResource $DSCResourcesCount = $DSCResources.Count
- Display the counts:
"$CommandCount commands available in PowerShellGet" "$DSCResourcesCount DSCResources available in PowerShell Gallery" "$ModuleCount Modules available in the PowerShell Gallery"
- Install the
TreeSize
module. As this is a public repository, Windows does not trust it by default, so you must approve installation or use-Force
:Install-Module -Name TreeSize -Force
- Review and test the commands in the module:
Get-Command -Module TreeSize Get-Help Get-TreeSize Get-TreeSize -Path C:\Windows\System32\Drivers -Depth 1 Uninstall the module: Uninstall-Module -Name TreeSize
- To inspect prior to installing, first create a download folder:
$NIHT = @{ ItemType = 'Directory' Path = "$env:HOMEDRIVE\DownloadedModules" } New-Item @NIHT
- Save the module to the folder:
$Path = "$env:HOMEDRIVE\DownloadedModules" Save-Module -Name TreeSize -Path $Path Get-ChildItem -Path $Path -Recurse | format-Table Fullname
- To test the downloaded
TreeSize
module, import it:$ModuleFolder = "$env:HOMEDRIVE\downloadedModules\TreeSize" Get-ChildItem -Path $ModuleFolder -Filter *.psm1 -Recurse | Select-Object -ExpandProperty FullName -First 1 | Import-Module -Verbose
How it works...
This recipe uses the cmdlets in the PowerShellGet
module to demonstrate how you can obtain and leverage modules and other PowerShell resources from the public PSGallery
site (https://www.powershellgallery.com/).
In step 1, you review the commands contained in the PowerShellGet
module, which looks like this:
Because the NuGet
package provider is required to use the PowerShell Gallery, you need to have this provider loaded. In step 2, you check the version of the provider, which looks like this:
PowerShellGet requires NuGet
provider version 2.8.5.201 or newer to interact with NuGet-based repositories, including PSGallery
. In this case, you have a later version of the NuGet
provider.
In step 3, you check what version of PowerShellGet
is currently installed on SRV1,
which looks like this:
In step 4, you install the latest version of the PowerShellGet
module from PSGallery
, which produces no output. In step 5, you view the versions of PowerShellGet
that are now available on SRV1,
like this:
In step 6, you examine the repositories PowerShell knows about (thus far), like this:
In step 7, you examine other providers contained in the PSGallery
, which you can download and use as needed:
To discover some of the things you can find using PowerShellGet
, in step 8 you get the commands in the module that use the Find
verb, like this:
There are a variety of resources you can obtain from the PSGallery
. In step 9, step 10, and step 11, you get the command, modules, and DSC resources respectively that are in the PSGallery
. This generates no output. In step 12, you display those counts, which looks like this:
In step 13, you install the TreeSize
module from the PSGallery
, which generates no output. In step 14, you look at the commands contained in the module (there is only one), then you run the command, which looks like this:
In step 15, you remove the module—this generates no output.
In some cases, you may wish to download the module to a separate folder to enable you to test the module before formally installing it. In step 16, you create a folder in your home drive, generating no output. Next, in step 17, you save the module into that folder and look at what's in the downloaded files folder, which looks like this:
In step 18, you load the module from the download folder. Using the -Verbose
switch enables you to see what Import-Module
is actually doing. The output is as follows:
Once you have imported the module you can then use either the Get-Treesize
function or its alias, TreeSize
.
There's more...
In step 3, you discover that the version of the PowerShellGet
module on the host is version 1.0.0.1 which ships with Windows 10. Since the initial release of Windows 10, PowerShellGet
has become a community-developed project, based at GitHub. The latest version of the module is available both from GitHub or via PSGallery
, with the latter being easier to work with for most IT pros. Visit the GitHub site to get more information: https://github.com/PowerShell/PowerShellGet.
In step 4, you added the latest version of the PowerShellGet
module onto your system and in step 5, you saw you now had two versions. PowerShell, by default, uses the later version, unless you explicitly load an earlier version prior to using the commands in the module.
In this recipe, you downloaded, used, and removed the TreeSize
module—one of thousands of modules you can freely download and use. Other popular modules in the PSGallery
include:
- Azure modules (including
MSOnline
): Azure provides a large number of smaller modules and most of these are frequently downloaded PowerShellGet
andPackageManagement
PSWindowsUpdate
PSSlack
IISAdministration
SQLServer
CredentialManager
Posh-SSH
See also…
For most IT pros, PSGallery
is the go-to location for obtaining useful modules that avoid you having to re-invent the wheel. In some cases, you may develop a particularly useful module (or script or DSC resource), which you can publish to the PSGallery
to share with others.
See https://docs.microsoft.com/en-us/powershell/gallery/concepts/publishing-guidelines for guidelines regarding publishing to the PSGallery
. And, while you are looking at that page, consider implementing best practices in any production script you develop.