Remove a PowerShell DSC Configuration

Trevor Sullivan picture Trevor Sullivan · Aug 16, 2014 · Viewed 12.8k times · Source

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;

Answer

Steven Murawski picture Steven Murawski · Aug 27, 2014

To move the 7zip installation out of scope, you can apply another configuration that does not specify 7zip. The configurations do not "roll back" so, it if was installed, it'll stay installed. If it is removed later, it will not be re-installed.

You can also go the manual route and delete the configuration document from c:\windows\system32\configuration. You'd need to remove Current.mof, backup.mof, and possibly Previous.mof.