# Printer Mapping

**URL:** <https://forums.powershell.org/t/printer-mapping/3792>\
**Category:** PowerShell Help\
**Created:** [February 23, 2015, 4:58am UTC](https://forums.powershell.org/t/printer-mapping/3792 "2015-02-23T04:58:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![netlocksecurity](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@netlocksecurity](https://forums.powershell.org/u/netlocksecurity)\
**Post date:** [February 23, 2015, 4:58am UTC](https://forums.powershell.org/t/printer-mapping/3792/1 "2015-02-23T04:58:50Z")

</div>

I’m currently borrowing from one of Bo Prox’s scripts to migrate printer shares and it works great! The only thing I’m having trouble with is logic to guarantee that although the print server name changes, the print share remains the default.

Any ideas?

Paul

```
Function Switch-Printer
{
	Param [
		$oldprintserver,
		$oldPrintbyIP,
		$newPrintServer,
		$PrinterLog = "c:\printers.csv"
	]
	

	Try
	{
		LogWrite ["{0}: Checking for printers mapped to old print server" -f $Env:USERNAME]
		$printers = @[Get-WmiObject -Class Win32_Printer | where { $_.Systemname -like "*$oldprintserver*" -or $_.SystemName -like "*$oldPrintbyIP*" }]
		
		If [$printers.count -gt 0]
		{
			ForEach [$printer in $printers]
			{
				LogWrite ["{0}: Replacing with new print server name: {1}" -f $Printer.Name, $newPrintServer]
				
				
				If [$printer.name -like "*$oldprintserver*"] { $newPrinter = $printer.Name -replace $oldprintserver, $newPrintServer }
				
				If [$printer.name -like "*$oldPrintbyIP*"] { $newPrinter = $printer.Name -replace $oldPrintbyIP, $newPrintServer }
				
				
				$returnValue = [[wmiclass]"Win32_Printer"].AddPrinterConnection[$newPrinter].ReturnValue
				If [$returnValue -eq 0]
				{
					"{0},{1},{2},{3},{4},{5}" -f $Env:COMPUTERNAME,
					$env:USERNAME,
					$newPrinter,
					$returnValue,
					[Get-Date],
					"Added Printer" | Out-File -FilePath $PrinterLog -Append -Encoding ASCII
					LogWrite ["{0}: Removing" -f $printer.name]
					$printer.Delete[]
					"{0},{1},{2},{3},{4},{5}" -f $Env:COMPUTERNAME,
					$env:USERNAME,
					$printer.Name,
					$returnValue,
					[Get-Date],
					"Removed Printer" | Out-File -FilePath $PrinterLog -Append -Encoding ASCII
				}
				Else
				{
					LogWrite ["{0} returned error code: {1}" -f $newPrinter, $returnValue] -Verbose
					"{0},{1},{2},{3},{4},{5}" -f $Env:COMPUTERNAME,
					$env:USERNAME,
					$newPrinter,
					$returnValue,
					[Get-Date],
					"Error Adding Printer" | Out-File -FilePath $PrinterLog -Append -Encoding ASCII
				}
			}
		}
	}
	Catch
	{
		"{0},{1},{2},{3},{4},{5}" -f $Env:COMPUTERNAME,
		$env:USERNAME,
		"WMIERROR",
		$_.Exception.Message,
		[Get-Date],
		"Error Querying Printers" | Out-File -FilePath $PrinterLog -Append -Encoding ASCII
	}
	
	
}
```

---

<div class="post-metadata">

**Author:** ![jack-neff](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jack-neff](https://forums.powershell.org/u/jack-neff)\
**Post date:** [February 23, 2015, 12:43pm UTC](https://forums.powershell.org/t/printer-mapping/3792/2 "2015-02-23T12:43:19Z")

</div>

Get-Member is your friend! If you do a

```
$printer | Get-Member
```
 you’ll notice it has a property called ‘Default’ (contains true or false) and a method called ‘SetDefaultPrinter’. With these two items all you have to do is invoke the SetDefaultPrinter method on the newly created printer if the old printer object’s Default parameter was “true”.

```
if ($returnValue -eq 0){
    if ($printer.default){ Get-WmiObject -Class win32_printer -Filter "Name like '$newprinter'" | Invoke-WmiMethod -Name SetDefaultPrinter }
    ##Write to log##
    $printer.Delete()
}
```

---

<div class="post-metadata">

**Author:** ![netlocksecurity](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@netlocksecurity](https://forums.powershell.org/u/netlocksecurity)\
**Post date:** [February 23, 2015, 3:12pm UTC](https://forums.powershell.org/t/printer-mapping/3792/3 "2015-02-23T15:12:28Z")

</div>

Jack,

Thanks a mill, I’m going to try this tonight!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:46pm UTC](https://forums.powershell.org/t/printer-mapping/3792/4 "2024-05-16T20:46:21Z")

</div>


