Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete all printer from each user profile on PC.

Status
Not open for further replies.
Dec 19, 2007
1
US
Hi,

We are retiring our old workhorse print server and I need to delete the printers from each user’s profile on the PC desktops.

I have a vb script that will delete all printers from the profile logged in but I want to clear from all profiles on PC.

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Network = True")

For Each objPrinter in colInstalledPrinters
objPrinter.Delete_
Next


Any help would be great,

Iowahawkeye
 
Win32_Printer will not expose printer for other users so you will need to remove them as part of a login script.

markdmac's login script in the FAQ area has a section for removing printers and adding new ones or remapping to a new server I believe.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
you might put an if statement to specifically address that printer, if it exists then delete and leave it in the standard log in script until you think its ok to remove it (as in all users have had a chance to to log in) or just leave it in perminently - either way

just an idea
 
As DM4Ever has pointed out the easiest solution is to delete these via login script. Here is a link to my FAQ that has the code you need.

faq329-5798

If you have not already retired the old print server, then I would recommend you also download a copy of PrintMig3.1 from Microsoft to help you duplicate all of the printers and their shares on the new server.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
I have this script already in my toolbag... In case you're interested...

Code:
[green]' =======================================================================
'
' NAME: UpdatePrintQueues-v1b.vbs
'
' AUTHOR: Paul S. Chapman
' DATE  : 7-5-2006
'
' COMMENT: This script was built to allow administrators to migrate print
' queues from one server to another with no disruption to the end user.
' Uses RegistryItemExists function FAQ'd by djhawthorne on tek-tips:
' FAQ329-5864
'==========================================================================[/green]

Option Explicit

Dim strFlagsOn, arrDefault, strDefaultPrinter
Dim objShell, objNetwork, objDict

Const strDPKey = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"

[green]' Check if command line argument "1" is passed to script.  Enable verbose
' mode if argument is present.[/green]
If WScript.Arguments.Count = 0 Then
	strFlagsOn = False
Elseif WScript.Arguments.Item(0) = "1" Then
	strFlagsOn = True
End If

Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
Set objDict = CreateObject("Scripting.Dictionary")

[green]' Log script start time[/green]
objShell.LogEvent 4,"Update Printers Script Starting"

[green]' Check if Default printer registry key exists.  If not then quit
' because this is the first time the user has logged in.[/green]
If RegItmExist(strDPKey) = False Then
	If strFlagsOn = True Then
		WScript.Echo "New User Profile.  Quitting..."
	End If
	WScript.Quit
End If

[green]' Read Registry key that contains default printer into an array[/green]
arrDefault = Split(objShell.RegRead(strDPKey), ",")

[green]' Verify that an array was returned, then check if default printer is network printer[/green]
If IsArray(arrDefault) Then
	If Left(arrDefault(0), 2) = "\\" Then
		strDefaultPrinter = arrDefault(0)
		If strFlagsOn = True Then
			WScript.Echo "Default Printer: " & strDefaultPrinter
		End If
	End If
End If

[green]' Populate Dictionary object with old and new printer names.  Old printer
' is the key, and new printer is the item (or definition).
' Syntax:
' .Add "\\<server>\<Old Queue Name>", "\\<server>\<New Queue Name>"
' ********** BEGIN EDIT AREA **********[/green]
With objDict
	.CompareMode = vbtextcompare
	.Add "\\OldServer\OldQueue1", "\\NewServer\NewQueue1"
	.Add "\\OldServer\OldQueue2", "\\NewServer\NewQueue2"
End With
[green]' ********** END EDIT AREA **********

[green]' Call sub to fix printers that are currently installed[/green]
FixInstalled

[green]' Reset Default printer, if it was replaced by "FixInstalled" sub.[/green]
If objDict.Exists(strDefaultPrinter) Then
	objNetwork.SetDefaultPrinter objDict.Item(strDefaultPrinter)
End If

[green]' Log script end time[/green]
objShell.LogEvent 4,"Update Printers Script Finished"


Sub FixInstalled
[green]' This sub enumerates the currently installed printers, then checks
' each one in turn against the Dictionary object to see if it has been
' replaced.  If printer exists in dictionary, delete it and install the
' replacement printer.[/green]
	Dim ctr, objPrinters, strPrinter
	Set objPrinters = objNetwork.EnumPrinterConnections

	[green]' Check if printer is installed before attempting to delete.[/green]
	For Ctr = 0 To objPrinters.Count - 1 Step 2
		If objDict.Exists(objPrinters.Item(ctr + 1)) Then
			strPrinter = objPrinters.Item(ctr + 1)
			If strFlagsOn = True Then
				WScript.Echo "		Removing dead printer:" & strPrinter
			End If
			objNetwork.RemovePrinterConnection strPrinter
			If strFlagsOn = True Then
				WScript.Echo "		Installing: " & objDict.Item(strPrinter)
			End If
			objNetwork.AddWindowsPrinterConnection objDict.Item(strPrinter)
		End If
	Next

End Sub

Function RegItmExist (RegistryItem)
' This function checks to see if registry key/value passed to it exists,
' and returns true if it does.

	If strFlagsOn = True Then
		WScript.Echo "	Checking registry for: " & RegistryItem
	End If

	Dim ErrDescription
	On Error Resume Next

	'Find out if we are looking for a key or a value
	If (Right(RegistryItem, 1) = "\") Then
		'It's a registry key we are looking for. Try reading the key.
		objShell.RegRead RegistryItem

		Select Case Err
			Case 0:
				RegItmExist = True

			Case &h80070002:
				ErrDescription = Replace(Err.description, RegistryItem, "")
				Err.clear
				objShell.RegRead "HKEY_ERROR\"

				If (ErrDescription <> Replace(Err.description, "HKEY_ERROR\", "")) Then
					RegItmExist = True
				Else
					RegItmExist = False
				End If

			Case Else:
				RegItmExist = False
		End Select

	Else
		'It's a registry value we are looking for. Try reading the value
		objShell.RegRead RegistryItem

		'Catch the Error
		Select Case Err
			Case 0:
			RegItmExist = True

			Case Else
			RegItmExist = False
		End Select

	End If
	
	'Turn error reporting back On
	On Error Goto 0
	If strFlagsOn = True Then
		WScript.Echo "		Item exists: " & RegItmExist
	End If
End Function

Good Luck!

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top