Forum Discussion

Belezeebub1974's avatar
Belezeebub1974
Copper Contributor
Jun 13, 2024

I need help with a Powershell Script I need to delete any Registry keys, values or Dwords

We updated our print server. I need to remove any Values, Friendly names, or DWORDs that reference the old print server. The old printers were pushed out via GPO by username, and we have computers with up to 50 copies of the same printer, I already tried editing the deployment scripts that did not remove them. I found a few scripts, but none of them seem to remove all of the entries

Here is one of the scrips I am trying to edit

$PrinterReg = Get-ChildItem -Path Registry::HKLM\SYSTEM\CurrentControlSet\Enum\SWD\PRINTENUM\*
$PrinterName = "\\Starbase16\*"
Foreach ($DeletePrinter in $PrinterReg){

$FriendlyName = $DeletePrinter.GetValue("Friendlyname")

if($FriendlyName -eq $PrinterName){

Remove-Item -path $DeletePrinter.PSPath -Recurse
Write-Host "Removing $friendlyname from $($DeletePrinter.PSPath)"

}}

Here are all the Keys that contain Starbase16 

Computer\HKEY_CURRENT_USER\Printers\ConvertUserDevModesCount
Computer\HKEY_CURRENT_USER\Printers\Settings
Computer\HKEY_CURRENT_USER\Software\Microsoft\Terminal Server Client\Servers\Starbase16
Computer\HKEY_CURRENT_USER\Software\Xerox\PrinterDriver\V5.0\``Starbase16`2E-Main-Mailroom
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\PrinterMigrationEx\CSR|Starbase16
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\Starbase16
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers\Client Side Rendering Print Provider\S-1-5-21-117609710-602162358-725345543-28548\Printers\Connections
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers\Client Side Rendering Print Provider\Servers\Starbase16\Monitors\Client Side Port\
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers\Client Side Rendering Print Provider\Servers\Starbase16\Printers\

  • Belezeebub1974's avatar
    Belezeebub1974
    Copper Contributor
    Co worker did this its better but it is still ignoring all the old printers under
    Key Computer\HKEY_CURRENT_USER\Printers\ConvertUserDevModesCount


    # Removes all registry values with a name that matches $TargetValueName from the
    # registry key specified for $RegPath. Use of wildcards is supported in
    # $TargetValue
    #
    # Logs all deleted keys in the file and path specified. Do not put a trailing \
    # at the end of the path.

    # Set Variables here
    $RegPath = 'Registry::HKU\.DEFAULT\Printers\ConvertUserDevModesCount'
    $TargetValueName = "*Starbase16*"
    $LogPath = "C:\LOG"
    $LogFile = "PrintersDeletedFromRegistry.txt"

    # Working script below.
    # Checks if log path exists, and creates if missing.
    $PrinterReg = Get-ItemProperty -Path $RegPath | Get-Member
    if (-not (Test-Path $LogPath)) {
    New-Item -ItemType Directory -Path $LogPath
    }

    # Searches Value Names for matching text, deletes, and logs.
    $FullPath = $LogPath+'\'+$LogFile
    Foreach ($RegKey in $PrinterReg){
    if ($RegKey -like $TargetValueName) {
    Write-Host $RegKey
    Add-Content -Path $FullPath -Value $RegKey.Name
    Remove-ItemProperty -Path $RegPath -Name $RegKey.Name
    }
    }
    • Harm_Veenstra's avatar
      Harm_Veenstra
      MVP
      I think you should use something like this:

      Get-Printer | Where-Object ComputerName -Match Starbase16 | Remove-Printer -Confirm:$false

      This will get all printers where the computername matches the old printer and removes them.

Resources