Remove Recent Printers from Chrome Preferences File (PoSH)

Share on:

Chrome does not remove "Recent Printers" from the print option box when a user or an administrator deletes a printer from the machine. This can cause problems when a printer is removed by policy or is updated with a new print server name, but a user continues totry and print to the old server. This script will remove the printer from the PREFERENCES file. You can provide no printer list and all Recent Printers will be removed, or you can specific a specific printer.

  1<#	
  2	===========================================================================
  3	 Created on:   	4/25/2017 4:00 PM
  4	 Created by:   	Christopher Kibble
  5	 Filename:     	RemoveChromeRecentPrinter.psm1
  6	 Version:		1.0
  7	-------------------------------------------------------------------------
  8	 Module Name: Remove-ChromeRecentPrinter
  9	===========================================================================
 10
 11	*******************************************************************************************************************************
 12	** All code is for demonstration only and should be used at your own risk. I cannot accept liability for unexpected results. **
 13	*******************************************************************************************************************************
 14
 15		   Use: You're welcome to use, modify, and distribute this script.  When possible, it'd be great if
 16				you'd credit me as the original author or a contributer, as well as those in the Credit 
 17				section below.  I'd also love to hear about how you're using it or modifications you've made
 18				in the comments section of the original post over at ChristopherKibble.com.
 19
 20	    Credit: While the script is entirely my work, the idea to modify the Preferences file, as well
 21				as where the settings that needed to be modified came from "Baronwab" from the Chromium
 22				bugs forum "Monorail".
 23
 24					* Baronwab: https://bugs.chromium.org/u/310329382/
 25					* Bug Thread: https://bugs.chromium.org/p/chromium/issues/detail?id=375238
 26	
 27	   Purpose:	Chrome does not remove "Recent Printers" from the print option box when a user or an
 28				administrator deletes a printer from the machine.  This can cause problems when a printer
 29				is removed by policy or is updated with a new print server name, but a user continues to
 30				try and print to the old server.  
 31
 32				This script will remove the printer from the PREFERENCES file.  You can provide no printer
 33				list and all Recent Printers will be removed, or you can specific a specific printer.  As
 34				the function uses the -like parameter when looking for printers, you can also supply a
 35				wildcarded list of printers (e.g. @("\\OLDSERVER\HR*","\\OLDSERVER\INFOSEC*")
 36
 37	 Liability: This script was created in a lab for a specific group of computers.  While it's likely to
 38				work "in the wild" on other systems without issue, it's not supported or endorsed by Google, 
 39				and may cause unintended and negative results.  PLEASE TEST BEFORE USING.  
 40
 41	*******************************************************************************************************************************
 42	** All code is for demonstration only and should be used at your own risk. I cannot accept liability for unexpected results. **
 43	*******************************************************************************************************************************
 44
 45#>
 46
 47<#
 48	.SYNOPSIS
 49		Removes Some or All Recent Printers from Chrome Preferences File
 50	
 51	.DESCRIPTION
 52		This script processes either the current user's default Chrome preferences file, or a preferences file defined by the executor, and removes recenter printers from the print preview list.
 53	
 54	.PARAMETER PreferenceFile
 55		Path to the Chrome preferences file
 56	
 57	.PARAMETER Printers
 58		Array of printers to search for and remove.  Wildcards accepted.
 59	
 60	.PARAMETER AbortOnChomeProcess
 61		Terminates script if Chrome is running, given the risk that any changes made will be undone when Chrome rewrites the preferences file when closed.
 62	
 63	.EXAMPLE
 64		PS C:\> Remove-ChromeRecentPrinter -PreferenceFile "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Preferences" -Printers @("\\OLDSERVER\PRINTER1","\\OLDSERVER\HR2")
 65	
 66	.NOTES
 67		If this is executed while Chrome is running, it's possible that Chrome will rewrite the preferences file when it closes and put the printers back.
 68#>
 69function Remove-ChromeRecentPrinter {
 70	[CmdletBinding(PositionalBinding = $true,
 71				   SupportsShouldProcess = $true)]
 72	param
 73	(
 74		[Parameter(Mandatory = $false,
 75				   Position = 1)]
 76		[Alias('pref', 'path')]
 77		[String]$PreferenceFile = "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Preferences",
 78		[Parameter(Position = 2)]
 79		[Array]$Printers,
 80		[Alias('NoChrome')]
 81		[switch]$AbortOnChomeProcess
 82	)
 83	
 84	if ($AbortOnChomeProcess) {
 85		if (Get-Process "Chrome" -ErrorAction SilentlyContinue) {
 86			Write-Error "Chrome process is running."
 87			Break
 88		} else {
 89			Write-Verbose "Chrome process is not running."	
 90		}
 91	}
 92	
 93	# Import Chrome Preferences File.
 94	Write-Verbose "Reading $PreferenceFile"
 95	$prefs = Get-Content -Path $PreferenceFile | ConvertFrom-Json -ErrorAction Stop
 96	
 97	# Get the Current Print Preview AppState Node (can't reference directly past appState) 
 98	Write-Verbose "Capturing the Print Preview Sticky Settings AppState"
 99	$appState = $prefs.printing.print_preview_sticky_settings.appState
100	
101	# Convert to an object we can manipulate
102	Write-Verbose "Converting the AppState from JSON"
103	$appState = $appState | ConvertFrom-Json -ErrorAction Stop
104	
105	Write-Verbose "Current Recent Destinations: $($appState.recentDestinations | Out-String)"
106	
107	# Remove Any Printers from Recent Destinations that match our rules
108	if ($Printers.Count -gt 0) {
109		
110		$Printers | ForEach-Object {
111			$removeItem = $_
112			Write-Verbose "Removing $removeItem"
113			$appState.recentDestinations = $appState.recentDestinations | Where-Object {
114				$_.Id -notlike $removeItem
115			}
116		}
117		
118	} else {
119		
120		Write-Verbose "Removing All Recent Destinations"
121		$appState.recentDestinations = ""
122		
123	}
124	
125	Write-Verbose "Updated Recent Destinations: $($appState.recentDestinations | Out-String)"
126	
127	# Convert the Object Back to JSON
128	Write-Verbose "Converting AppState back to JSON"
129	$appStateJSON = $appState | ConvertTo-Json -Compress -ErrorAction Stop
130	
131	# Change the AppState from the preferences object
132	Write-Verbose "Updating In Memory Printer Sticky Settings AppState"
133	$prefs.printing.print_preview_sticky_settings.appState = $appStateJSON
134	
135	# Conver The Preferences Back to JSON and write it back out to the preferences file.
136	Write-Verbose "Writing Preferences Back to File"
137	$prefs | ConvertTo-Json -Depth 99 -Compress | Out-File $PreferenceFile -Encoding default -ErrorAction Stop
138}
139
140Export-ModuleMember -Function Remove-ChromeRecentPrinter


6 comments

Jonathan Tremblay

I get the following error:

The property ????PositionalBinding???? is unavailable for this type ????System.Management.Automation.CmdletBindingAttribute????

. At the level: C:\Windows\system32\WindowsPowerShell\v1.0\Modules\ClearCachePrinterChrome\ClearCachePrinterChrome.psm1??: 72 Caract??re??: 16 [CmdletBinding <<<< (PositionalBinding = $true, CategoryInfo : InvalidOperation: (:) [], RuntimeException FullyQualifiedErrorId : PropertyAssignmentException

Jonathan Tremblay

Forget it... i found the issue... you need powershell v4. By default, Windows 7 has powershell v2.

J.T. Brown III

Just figured out how to install and run this script on W10: One must:

  1. Save the above code as the following file: RemoveChromeRecentPrinter.psm1
  2. Copy the above file to: C:\Program Files\WindowsPowerShell\Modules
  3. Close/Exit Google Chrome browser (and then backup your user profile directory just in case of an issue!).
  4. Open a PowerShell window and run the command: Remove-ChromeRecentPrinter (plus any of the optional parameters listed above in the code).
  5. Reopen Chrome and verify that the 'Recent Printers' have been removed.

The above worked great on my W10 machines. Thank you Christopher Kibble!

Chris Kibble

Hey J.T. ??? thanks for your comment and sorry I???m late responding! Glad you got it working!

Frank

Good Afternoon,

after removing the printers the " Save as PDF" gets disabled. not sure how to get it back