Forum Discussion
Belezeebub1974
Jun 13, 2024Copper Contributor
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 wi...
Belezeebub1974
Jun 13, 2024Copper 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
}
}
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
}
}
Jun 17, 2024
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.
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.