Getty Images

Use PSResourceGet to manage PowerShell modules and scripts

The updated package manager for PowerShell improves performance, simplifies module management and streamlines repository handling to free up time for automation tasks.

Managing PowerShell resources is a critical task for administrators, who must ensure that scripts and modules are up to date and correctly configured across environments.

With the release of PSResourceGet, Microsoft has improved upon the original PowerShellGet module by producing a modernized approach to managing PowerShell resources. This article looks at some of PowerShellGet's limitations, the problems solved by PSResourceGet and its key capabilities, and practical examples of how to use PSResourceGet in daily workflows.

Problems that PSResourceGet Solves

As useful as PowerShellGet has been, PSResourceGet was developed to address several key issues, including:

  • Complexity and performance issues. Microsoft designed PowerShellGet with a provider model that relied on the PackageManagement module, which led to bottlenecks and added complexity in managing dependencies.
  • Usability and extensibility. Because it was based on an older codebase, PowerShellGet was neither user-friendly nor easy to extend, making bug fixes and implementing new features difficult.
  • Compatibility concerns. Because it was written in PowerShell, keeping PowerShellGet compatible with all supported versions of PowerShell was a challenge.

By rewriting PowerShellGet's replacement in C# and removing its dependency on PackageManagement, PSResourceGet offers a more efficient and maintainable approach.

PSResourceGet capabilities

As the new package manager for PowerShell, PSResourceGet is a comprehensive tool to manage all types of PowerShell artifacts available from repositories, including modules, scripts, Desired State Configuration resources and role capabilities.

For each resource type, PSResourceGet lets administrators:

  • Install, update and remove resources.
  • Manage dependencies automatically.
  • Manage repositories that provide resources.
  • Search repositories for resources.

All capabilities are exposed in the module through cmdlets.

Benefits of PSResourceGet

Now that we've covered the purpose of PSResourceGet and its capabilities, let's cover the advantages of using PSResourceGet over PowerShellGet.

Improved performance

The most significant and immediate benefit of PSResourceGet is speed. For example, the following script compares the search performance of both modules:

Measure-Command {
    Find-Module -Name Microsoft.PowerShell.PSResourceGet
} | Select-Object TotalSeconds
Measure-Command {
    Find-PSResource -Name Microsoft.PowerShell.PSResourceGet
} | Select-Object TotalSeconds

After running these commands a few times, PSResourceGet typically executes twice as fast as PowerShellGet. Results can vary depending on the testing environment, including the host machine and internet connection.

Compatibility

To maintain compatibility with previous versions of PowerShellGet, Microsoft released a compatibility module designed to accept commands in the same syntax as PowerShellGet and call the equivalent PSResourceGet commands. This approach lets administrators continue running scripts that use PowerShellGet syntax while also benefiting from PSResourceGet enhancements.

Maintainability and extensibility

Although this might not be immediately noticeable for most users, one goal for PSResourceGet was to build a module that could easily implement changes based on customer feedback. From the start, the codebase was written to be clean and efficient so Microsoft could quickly address bugs and feature requests.

Using PSResourceGet

To start using PSResourceGet, you might have to install it. For all versions of PowerShell prior to 7.4, PSResourceGet isn't included by default, so we will use PowerShellGet's Install-Module cmdlet for the installation:

Install-Module -Name Microsoft.PowerShell.PSResourceGet

After installation, you can verify by importing it, running a command from the module or simply listing the commands in the module:

Get-Command -Module Microsoft.PowerShell.PSResourceGet

Managing repositories

A repository is a location to publish PowerShell resources and retrieve them. The most well-known is the PowerShell Gallery. Using PSResourceGet, you can find the currently configured repositories:

Get-PSResourceRepository

By default, PowerShell only includes the PowerShell Gallery. To add other repositories, such as an internal private repository hosted in GitHub Packages, use the following code:

$splat = @{
    Name     = 'GitHubOrg'
    Uri      = 'https://nuget.pkg.github.com/<GitHubOrg>/index.json'
    Trusted  = $true
    Priority = 10
}
Register-PSResourceRepository @splat

In this case, the script designates the repository as trusted and assigns it to a higher priority than the PowerShell Gallery.

You can verify the change by rerunning Get-PSResourceRepository.

A command window shows a PowerShell command used to list repositories registered for use with PSResourceGet.
Re-run the Get-PSResourceRepository command after registering a GitHub repository as a trusted source for PowerShell resources.

To see the full list of supported repository types, you can refer to Microsoft's documentation.

Installing modules

The most common use for PSResourceGet is to install modules. This is very similar to using Install-Module, but instead use:

Install-PSResource -Name Microsoft.Graph

If you want a specific version, PSResourceGet has a lot more flexibility than PowerShellGet. The Version parameter accepts any valid Nuget version range, while PowerShellGet required three version parameters. For example, to install an exact version, use the following command:

Install-PSResource -Name Microsoft.Graph -Version 2.21.0

To specify a version range, such as greater than 1.5 but less than 2.0, you can use:

Install-PSResource -Name Microsoft.Graph -Version '[1.5,2.0)'

To validate your version range, use Find-PSResource.

A command window runs a PowerShell command to find a module between a specific version range.
The command checks for versions of the Microsoft.Graph module that are at least version 1.5 but less than version 2.0.

PSResourceGet will install the latest version listed in the provided range.

Installing modules across several servers

To ensure the entire environment has the appropriate modules installed, you can use PSResourceGet through PowerShell remoting. For example, to manage Windows updates, you might want to install the PSWindowsUpdate module. The following script -- which requires PowerShell v7 because it uses the Parallel parameter of Foreach-Object -- retrieves a list of servers from Active Directory and then installs the module remotely:

Get-ADComputer -Filter 'OperatingSystem -like "*Server*"' | ForEach-Object -ThrottleLimit 5 -Parallel {
    Invoke-Command -ComputerName $_.Name -ScriptBlock {
        Install-Module -Name Microsoft.PowerShell.PSResourceGet -Confirm:$false -Force
        Install-PSResource -Name PSWindowsUpdate -Confirm:$false
    }
}

The script will install PSResourceGet if it isn't installed with Install-Module, then it will install PSWindowsUpdate with Install-PSResource.

Browse from the command line

Because PSResourceGet is so much faster than PowerShellGet, browsing the PowerShell Gallery from the command line makes more sense. For example, if you know there's a module to import Excel files but can't remember the name, you can search for it:

Find-PSResource -Name '*Excel'
A command window with a PowerShell command that searches for resources using a wildcard.
Use the Find-PSResource command to search for PowerShell resources in the registered repositories with 'Excel' at the end of the name.

This command returns several modules, one of which is ImportExcel. To see the full metadata for the module, query it directly:

Find-PSResource -Name 'ImportExcel' | fl *
A command window that shows a PowerShell command to display a module's metadata.
Execute a search to that displays all the metadata for the ImportExcel module.

The output shows important information, such as the ProjectUri property, which you can use to verify it is the proper module.

PSResourceGet offers scalability and reliability

PSResourceGet offers improved package management in PowerShell, providing better speed and reliability. By directly addressing user feedback and the issues with PowerShellGet, PSResourceGet presents a more streamlined and efficient approach to reduce time spent on repetitive tasks and allow more time to focus on automation and scripting.

Anthony Howell is an IT strategist with extensive experience in infrastructure and automation technologies. His expertise includes PowerShell, DevOps, cloud computing, and working in both Windows and Linux environments.

Dig Deeper on IT operations and infrastructure management