PowerShell Problem Solver: Cleaning Up Old DSC Resources


Desired State Configuration (DSC) is a fascinating technology that can dramatically alter an IT pro’s job description. This is a big topic, but today I want to focus on one small piece, and one that I have found a little annoying. One of the elements that makes DSC work is a DSC Resource. A resource is a configuration element that is defined generally through PowerShell script and packaged as a PowerShell module. In the beginning we only had a small number of resources and everything was essentially version 1.0. Now there are many, many resources, often in varying stages of maturity as reflected by version number.



You can find and install many of these resources from the PowerShell Gallery.

Finding DSC Resources

The challenge is you can download new versions and end up with multiple versions locally. For example, look at a single DSC Resource I have installed.

Multiple versions installed

When I go to use the resource in a DSC Configuration I am presented with a related error condition.

Configuration Error

If I hover over the red squiggle, I’ll get a message about multiple resource versions detected. The solution is simple enough. I merely have to explicitly state which resource module to import.

This is probably for the best because I can guarantee exactly which version to use. This is also a good practice if I’m creating configurations I plan to share. But if I always keep my resources up-to-date with the latest version, I can save myself a little time. So what I need is a tool to prune my installed DSC Resources, always keeping the most current version and uninstalling everything else. There is one big caveat here: some DSC Resources take dependencies on other resources and often a specific version. PowerShell doesn’t like it if you try to uninstall a dependent resource. In addition, your configurations might need to be updated to work with newer versions.  But if you are good, or merely want to see how you can find and remove older DSC resources, then you can use this PowerShell function.

The function gets all DSC Resources and groups them by module name and then selects those with more than result. It then sorts each group by version number and uninstalls all but the most recent. I can cleanup a single resource:

Clean up a single resource

Or all of them.

Clean up all

I’ll go ahead and re-run the command without -WhatIf to clean up. I have flagged the removal process with a high impact so that you will be prompted to remove. And if a dependency is detected the module won’t be uninstalled.

Removing older resources

If, for some reason, I find I need a specific resource version, I can download it again from the PowerShell Gallery, so there shouldn’t be that much risk in periodically cleaning up. Still, this is significant process so please, please, test and and plan accordingly.

Related Topics:

  • PowerShell

    Don't have a login but want to join the conversation? Sign up for a Petri Account