Reset-DSCResource.ps1

function Reset-DSCResource
{
    <#
    .Synopsis
        Removes automatically generated DSC resources
    .Description
        Removes resources that were generated based off of .resource.ps1 files.
    .Link
        Initialize-DSCResource
    .Example
        Reset-DSCResource $env:ProgramFiles\WindowsPowerShell\Modules\cFg
    #>

    [OutputType([Nullable])]
    [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='High')]
    param(
    # The root directory of the module.
    [Parameter(Mandatory=$true,Position=0,ValueFromPipelineByPropertyName=$true)]
    [Alias('Path')]
    [string]
    $ModuleRoot,

    # If provided, will not reset resources whose version number is less then the provided value
    [Version]
    $MinimumVersion,

    # If provided, will not reset resources whose version number is greater then the provided value
    [Version]
    $MaximumVersion
    )

    process {
        if ($_ -is [Management.Automation.PSModuleInfo]) {
            $ModuleRoot = $ModuleRoot | Split-Path
        }

        $resourcePs1Files = Get-ChildItem -Recurse -Filter *resource*.ps1 -Path $ModuleRoot               
        #region Find all Resource.ps1 files
        $ResourceData = 
            foreach ($rp in $resourcePs1Files) {
                if ($rp.Name -like "*-*") {
                    Write-Verbose "Skipping Potential Resource File $rp, because it looks like a function"
                    continue
                }
                $nameParts = $rp.Name.Split('.')

                $v = $nameParts[-6..-3] -join '.' -as [Version]
                $resourceName = $nameParts[0..($nameParts.Count - 7)] -join '.'
                if (-not $v) {
                    $v = $nameParts[-5..-3] -join '.' -as [Version]
                    $resourceName = $nameParts[0..($nameParts.Count - 6)] -join '.'
                }
                if (-not $v) {
                    $v = $nameParts[-4..-3] -join '.' -as [Version]
                    $resourceName = $nameParts[0..($nameParts.Count - 5)] -join '.'
                }
                if (-not $v) {
                    $v = '0.0' -as [Version]
                    $resourceName = $nameParts[0..($nameParts.Count - 2)] -join '.'
                }
            
            

                New-Object PSObject -Property @{
                    ResourceName = $resourceName
                    ResourcePath = $rp.Fullname
                    ResourceVersion = $v 
                }
            }
        #endregion Find all Resource.ps1 files

        foreach ($rd in $ResourceData) {
            if ($MinimumVersion -and $rd.ResourceVersion -lt $MinimumVersion) {
                continue
            }
            if ($maximumVersion -and $rd.ResourceVersion -gt $maximumVersion) {
                continue
            }
            
            
            $existingResourceFiles = Get-ChildItem -Path (Join-Path $moduleRoot DSCResources) -Filter $rd.ResourceName  -ErrorAction SilentlyContinue

            if ($PSCmdlet.ShouldProcess("Remove DSC Resource $($rd.ResourceName)")) {
                $existingResourceFiles | Remove-Item -Force -Recurse
            }

            
        }
    }
}