<# This script will clear the Active Setup Stubpath values. However, instead of just clearing them, it creates a backup of the Stubpath with the time stamp for future reference and troublshooting requirements. Script name: ClearActiveSetup.ps1 Release 1.2 Written by Jeremy Saunders (jeremy@jhouseconsulting.com) 19th September 2017 Modified by Jeremy Saunders (jeremy@jhouseconsulting.com) 25th September 2023 #> #------------------------------------------------------------- # Set Powershell Compatibility Mode Set-StrictMode -Version 2.0 # Enable verbose, warning and error mode $VerbosePreference = 'Continue' $WarningPreference = 'Continue' $ErrorPreference = 'Continue' #------------------------------------------------------------- $StartDTM = (Get-Date) # Get the TEMP path $logPath = [System.IO.Path]::GetTempPath() $logPath = $logPath.Substring(0,$logPath.Length-1) # Set the logpath to C:\Windows\Temp $logPath = "${env:SystemRoot}" + "\Temp" # Get the script name $ScriptName = [System.IO.Path]::GetFilenameWithoutExtension($MyInvocation.MyCommand.Path.ToString()) $logFile = "$logPath\$ScriptName.log" # Start the transcript try { Start-Transcript "$logFile" } catch { Write-Verbose "$(Get-Date -format "dd/MM/yyyy HH:mm:ss"): This host does not support transcription" } #------------------------------------ # Add strings to this array that may be in StubPaths that you want to be excluded from being cleared. $Excludes = @() $ParentPaths = @("HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components","HKLM:\SOFTWARE\WOW6432Node\Microsoft\Active Setup\Installed Components") $Value = "StubPath" $BackupValue = "StubPath.Backup-$(Get-Date -format "dd/MM/yyyy HH:mm:ss")" ForEach ($ParentPath in $ParentPaths) { Get-Childitem "$ParentPath" -Recurse -ErrorAction SilentlyContinue | % { $ValueExist = $False $ErrorActionPreference = "stop" Try { If ((Get-ItemProperty -Path "$ParentPath\$($_.PSChildName)" | Select-Object -ExpandProperty "$Value") -ne $null) { $ValueExist = $True } } Catch [System.Exception] { #$($_.Exception.Message) } $ErrorActionPreference = "Continue" If ($ValueExist) { Write-Verbose "`"$Value`" found under `"$ParentPath\$($_.PSChildName)`"" -verbose $BackupValueExist = $False $ErrorActionPreference = "stop" Try { If ((Get-ItemProperty -Path "$ParentPath\$($_.PSChildName)" | Select-Object -ExpandProperty "$BackupValue") -ne $null) { $BackupValueExist = $True } } Catch [System.Exception] { #$($_.Exception.Message) } $ErrorActionPreference = "Continue" If ($BackupValueExist -eq $False) { $ParentKey = Get-Item -Path "$ParentPath\$($_.PSChildName)" $PropertyValue = $ParentKey.GetValue("$Value") If ($PropertyValue -ne "") { Write-Verbose "- Value: $PropertyValue" -verbose $PropertyType = $ParentKey.GetValueKind("$Value") $Skip = $False ForEach ($Exlude in $Excludes) { If ($PropertyValue -Like "*$Exlude*") { $Skip = $True Break } } If ($Skip -eq $False) { Write-Verbose "- Type: $PropertyType" -verbose Write-Verbose "- Renaming to `"$BackupValue`"..." -verbose Rename-ItemProperty -Path "$ParentPath\$($_.PSChildName)" -Name "$Value" -NewName "$BackupValue" Write-Verbose "- Creating an empty `"$Value`" value with property type of `"$PropertyType`"..." -verbose New-ItemProperty -Path "$ParentPath\$($_.PSChildName)" -Name "$Value" -PropertyType $PropertyType -Value "" –Force | Out-Null } Else { Write-Verbose "- This $Value value has been excluded. No action will be taken." -verbose } } Else { Write-Verbose "- The value is empty. No action will be taken." -verbose } } Else { Write-Verbose "- The `"$BackupValue`" value already exists." -verbose } } } } #------------------------------------ Write-Verbose "Stop logging" -Verbose $EndDTM = (Get-Date) Write-Verbose "Elapsed Time: $(($EndDTM-$StartDTM).TotalSeconds) Seconds" -Verbose Write-Verbose "Elapsed Time: $(($EndDTM-$StartDTM).TotalMinutes) Minutes" -Verbose # Stop the transcript try { Stop-Transcript } catch { Write-Verbose "$(Get-Date -format "dd/MM/yyyy HH:mm:ss"): This host does not support transcription" }