Search code examples
powershelldsc

Remove a PowerShell DSC Configuration


Is it possible to remove a DSC configuration from a computer, once it has been applied?

For example, I have a configuration block as follows:

configuration SevenZip {
    Script Install7Zip {
        GetScript = {
            $7ZipFolder = '{0}\7-Zip' -f $env:ProgramFiles;
            $Result = @{
                Result = '';
                }
            if (Test-Path -Path $7ZipFolder) {
                $Result.Result = 'Present';
            }
            else {
                $Result.Result = 'Nonpresent';
            }
            Write-Verbose -Message $Result.Result;
            $Result;
        }
        SetScript = {
            $7ZipUri = 'http://colocrossing.dl.sourceforge.net/project/sevenzip/7-Zip/9.20/7z920-x64.msi';
            $OutputFile = '{0}\7-Zip.msi' -f $env:TEMP;
            Invoke-WebRequest -Uri $7ZipUri -OutFile $OutputFile;
            Write-Verbose -Message ('Finished downloading 7-Zip MSI file to {0}.' -f $OutputFile);

            $ArgumentList = '/package "{0}" /passive /norestart /l*v "{1}\Install 7-Zip 9.20 64-bit.log"' -f $OutputFile, $env:TEMP;
            $Process = Start-Process -FilePath msiexec.exe -ArgumentList $ArgumentList -Wait -PassThru;
            Write-Verbose -Message ('Process finished with exit code: {0}' -f $Process.ExitCode);
            Remove-Item -Path $OutputFile;
            Write-Verbose -Message ('Removed MSI file: {0}' -f $OutputFile);
        }
        TestScript = {
            $7ZipFolder = '{0}\7-Zip' -f $env:ProgramFiles;
            if (Test-Path -Path $7ZipFolder) {
                $true;
            }
            else {
                $false;
            }
        }
    }    
}

This configuration block will generate a MOF that uses a Script resource to install 7-Zip 9.20 64-bit, on the system to which it is applied, if the application is not found.

Once this configuration has been applied, using the below commands, how can it be removed from the system, if 7-Zip is no longer required?

SevenZip -OutputPath c:\dsc\7-Zip;
Start-DscConfiguration -Wait -Path C:\dsc\7-Zip -Verbose;

Solution

  • Remove-DscConfigurationDocument will do the trick https://technet.microsoft.com/en-us/library/mt143544.aspx

    The Remove-DscConfigurationDocument cmdlet removes a configuration document (.mof file) from the DSC configuration store. During configuration, the Start-DscConfiguration cmdlet copies a .mof file into to a folder on the target computer. This cmdlet removes that configuration document and does additional cleanup.