{"id":67,"date":"2017-06-18T15:57:05","date_gmt":"2017-06-18T19:57:05","guid":{"rendered":"http:\/\/ziviz.us\/WP\/?p=67"},"modified":"2017-06-18T16:13:17","modified_gmt":"2017-06-18T20:13:17","slug":"install-printer","status":"publish","type":"post","link":"https:\/\/www.ziviz.net\/WP\/2017\/06\/18\/install-printer\/","title":{"rendered":"Install-Printer"},"content":{"rendered":"<p style=\"text-align: center;\"><em>Code Release<\/em><\/p>\n<p>This script is intended to assist in SCCM deployments of optional printers. The intended effect is to allow a user to open Software Center or the SCCM Application Catalog and click an option such as &#8220;Map me to XXX Printer&#8221;, and have the printer, port, and driver all install automatically.<\/p>\n<h2>Parameters<\/h2>\n<p><b>DriverName &lt;Object&gt;<\/b> The name of the printer driver as defined in the INF file.<br \/>\n<b>DriverInf &lt;Object&gt;<\/b> Full path to the Driver INF file<br \/>\n<b>DriverPath &lt;Object&gt;<\/b> Path to the folder containing the driver files. (Default is the INF directory)<br \/>\n<b>PrinterIP &lt;Object&gt;<\/b> IP of the printer to map<br \/>\n<b>PrinterPort &lt;Object&gt;<\/b> Printer&#8217;s connection port. Default is 9100<br \/>\n<b>PrinterPortName &lt;Object&gt;<\/b> Name of the printer port. Defaults to &#8220;IP_&lt;IP&gt;&#8221;<br \/>\n<b>PrinterCaption &lt;Object&gt;<\/b> Name of the printer<br \/>\n<b>Remove [&lt;SwitchParameter&gt;]<\/b> Removes the specified printer instead of installing it<br \/>\n<b>DriverOnly [&lt;SwitchParameter&gt;]<\/b> Only installs the printer driver. Does not attempt to create the port or printer<br \/>\n<b>AllSteps [&lt;SwitchParameter&gt;]<\/b> Performs Driver install followed by the port and printer mappings. (Default without this and driver only is to only map a printer)<br \/>\n<b>WriteLog [&lt;SwitchParameter&gt;]<\/b> Writes Debug data to a log file. (Default is true)<br \/>\n<b>RemoveAll [&lt;SwitchParameter&gt;]<\/b> Removes all mapped printers. (You may need to run this once as the user, and once as an admin for full effect)<br \/>\n<b>AdditionalRemovalExclusions &lt;Object&gt;<\/b> When used with RemoveAll, prevents the removal of listed printers. By default, Send to One Note, and FAX are excluded.<\/p>\n<h2>Usage<\/h2>\n<h3>PowerShell Script<\/h3>\n<p>To add a printer:<\/p>\n<pre><code>&amp;\"\" -PrinterCaption \"My Printer\" -DriverName \"Xerox 7845 PS v4\" -PrinterIP \"192.168.1.2\"<\/code><\/pre>\n<p>To add a driver:<\/p>\n<pre><code>&amp;\"\" -DriverName \"Xerox 7845 PS v4\" -DriverInf \"\\\\Drivers\\Xerox\\7845\\Driver.inf\" -DriverOnly<\/code><\/pre>\n<p>To remove a printer and also disable logging<\/p>\n<pre><code>&amp;\"\" Install-Printer.ps1 -PrinterCaption \"My Printer\" -Remove -WriteLog:$false<\/code><\/pre>\n<p>To install a driver and then map a printer (Note that, in general, only administrators have the permissions to successfully use the script this way)<\/p>\n<pre><code>&amp;\"\" -PrinterCaption \"My Printer\" -DriverName \"Xerox 7845 PS v4\" -DriverInf \"\\\\Drivers\\Xerox\\7845\\Driver.inf\" -PrinterIP \"192.168.1.2\" -AllSteps<\/code><\/pre>\n<p>To remove all printers, or a selection of printers<\/p>\n<pre><code>&amp;\"\" -RemoveAll -AdditionalRemovalExclusions @(\"My Printer\", \"Send by E-mail printer\")<\/code><\/pre>\n<h3>SCCM Usage<\/h3>\n<p>To install a printer from SCCM, do the following.<\/p>\n<ol>\n<li>Create an application package for your driver. This can be an executable install or an INF install. If you are using an INF file, you may use the Install-Printer script to help.\n<figure id=\"attachment_69\" aria-describedby=\"caption-attachment-69\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-69\" src=\"http:\/\/ziviz.us\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_Programs_Tab-300x257.png\" alt=\"\" width=\"300\" height=\"257\" srcset=\"https:\/\/www.ziviz.net\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_Programs_Tab-300x257.png 300w, https:\/\/www.ziviz.net\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_Programs_Tab.png 641w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><figcaption id=\"caption-attachment-69\" class=\"wp-caption-text\">Example of the Driver Installation Programs Tab<\/figcaption><\/figure>\n<ol>\n<li>To create an INF file Application deployment. Create a new application deployment. Set the command to\n<pre><code>&amp;\"Install-Printer.ps1\" -DriverOnly -DriverName '' -DriverINF ''<\/code><\/pre>\n<figure id=\"attachment_72\" aria-describedby=\"caption-attachment-72\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-72\" src=\"http:\/\/ziviz.us\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_Detect_Tab-300x257.png\" alt=\"\" width=\"300\" height=\"257\" srcset=\"https:\/\/www.ziviz.net\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_Detect_Tab-300x257.png 300w, https:\/\/www.ziviz.net\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_Detect_Tab.png 642w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><figcaption id=\"caption-attachment-72\" class=\"wp-caption-text\">Example of the Driver Installation Detect Tab<\/figcaption><\/figure>\n<figure id=\"attachment_74\" aria-describedby=\"caption-attachment-74\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-74\" src=\"http:\/\/ziviz.us\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_User_Experience_Tab-300x258.png\" alt=\"\" width=\"300\" height=\"258\" srcset=\"https:\/\/www.ziviz.net\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_User_Experience_Tab-300x258.png 300w, https:\/\/www.ziviz.net\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_User_Experience_Tab.png 639w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><figcaption id=\"caption-attachment-74\" class=\"wp-caption-text\">Example of the Driver User Experience Tab<\/figcaption><\/figure>\n<p><figure id=\"attachment_75\" aria-describedby=\"caption-attachment-75\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-75\" src=\"http:\/\/ziviz.us\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_Detect_Script-300x295.png\" alt=\"\" width=\"300\" height=\"295\" srcset=\"https:\/\/www.ziviz.net\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_Detect_Script-300x295.png 300w, https:\/\/www.ziviz.net\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_Detect_Script.png 525w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><figcaption id=\"caption-attachment-75\" class=\"wp-caption-text\">Example of the Driver Installation Detect Script<\/figcaption><\/figure><\/li>\n<li>For the detection method, us the &#8220;Driver Detection&#8221; script in the &#8220;script&#8221; section of this page.<\/li>\n<\/ol>\n<\/li>\n<li>Create an application package for your printer.\n<ol>\n<li>Create a new Application Deployment. Set the command to &#8220;Install-Printer.ps1&#8221; -PrinterCaption &#8216;My Printer&#8217; -DriverName &#8216;Xerox 7845 PS v4&#8217; -PrinterIP &#8216;192.168.1.2&#8217;\n<p><figure id=\"attachment_77\" aria-describedby=\"caption-attachment-77\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-77\" src=\"http:\/\/ziviz.us\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_Programs_Tab-1-300x257.png\" alt=\"\" width=\"300\" height=\"257\" srcset=\"https:\/\/www.ziviz.net\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_Programs_Tab-1-300x257.png 300w, https:\/\/www.ziviz.net\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_Programs_Tab-1.png 641w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><figcaption id=\"caption-attachment-77\" class=\"wp-caption-text\">Example of the Printer Installation Programs Tab<\/figcaption><\/figure><\/li>\n<li>Set the detection method to the &#8220;Printer Detection&#8221; script in the &#8220;script&#8221; section of this page.\n<p><figure id=\"attachment_78\" aria-describedby=\"caption-attachment-78\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-78\" src=\"http:\/\/ziviz.us\/WP\/wp-content\/uploads\/2017\/06\/PD-Printer_Detect_Script-300x293.png\" alt=\"\" width=\"300\" height=\"293\" srcset=\"https:\/\/www.ziviz.net\/WP\/wp-content\/uploads\/2017\/06\/PD-Printer_Detect_Script-300x293.png 300w, https:\/\/www.ziviz.net\/WP\/wp-content\/uploads\/2017\/06\/PD-Printer_Detect_Script.png 528w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><figcaption id=\"caption-attachment-78\" class=\"wp-caption-text\">Example of the Printer Installation Detect Script<\/figcaption><\/figure><\/li>\n<li>Set this package to install for the user.\n<p><figure id=\"attachment_79\" aria-describedby=\"caption-attachment-79\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-79\" src=\"http:\/\/ziviz.us\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_User_Experience_Tab-1-300x258.png\" alt=\"\" width=\"300\" height=\"258\" srcset=\"https:\/\/www.ziviz.net\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_User_Experience_Tab-1-300x258.png 300w, https:\/\/www.ziviz.net\/WP\/wp-content\/uploads\/2017\/06\/PD-Driver_User_Experience_Tab-1.png 639w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><figcaption id=\"caption-attachment-79\" class=\"wp-caption-text\">Example of the Printer Installation User Experience Tab<\/figcaption><\/figure><\/li>\n<li>Add a requirement that points to the appropriate driver application package that you created earlier.\n<p><figure id=\"attachment_80\" aria-describedby=\"caption-attachment-80\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-80\" src=\"http:\/\/ziviz.us\/WP\/wp-content\/uploads\/2017\/06\/PD-Printer_Dependency_Tab-300x256.png\" alt=\"\" width=\"300\" height=\"256\" srcset=\"https:\/\/www.ziviz.net\/WP\/wp-content\/uploads\/2017\/06\/PD-Printer_Dependency_Tab-300x256.png 300w, https:\/\/www.ziviz.net\/WP\/wp-content\/uploads\/2017\/06\/PD-Printer_Dependency_Tab.png 646w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><figcaption id=\"caption-attachment-80\" class=\"wp-caption-text\">Example of the Printer Installation Dependency Tab<\/figcaption><\/figure><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<h2>Script<\/h2>\n<h3>Install-Printer<\/h3>\n<pre><code>&lt;#\r\n.SYNOPSIS\r\n    Provides helpfull script to add printers to a machine\r\n \r\n.DESCRIPTION\r\n    Provides a method to add a printer, driver, and port to a machine\r\n \r\n.PARAMETER DriverName\r\n    The name of the printer driver as defined in the INF file.\r\n \r\n.PARAMETER DriverInf\r\n    Full path to the Driver INF file\r\n \r\n.PARAMETER DriverPath\r\n    Path to the folder containing the driver files. (Default is the INF directory)\r\n \r\n.PARAMETER PrinterIP\r\n    IP of the printer to map\r\n \r\n.PARAMETER PrinterPort\r\n    Printer's connection port. Default is 9100\r\n \r\n.PARAMETER PrinterPortName\r\n    Name of the printer port. Defaults to \"IP_\". If you need to map the printer to a default local port, use the name of the port instead. Such as \"FILE:\".\r\n \r\n.PARAMETER PrinterCaption\r\n    Name of the printer\r\n \r\n.PARAMETER Remove\r\n    Removes the specified printer instead of installing it\r\n \r\n.PARAMETER DriverOnly\r\n    Only installs the printer driver. Does not attempt to create the port or printer\r\n \r\n.PARAMETER AllSteps\r\n    Performs Driver install followed by the port and printer mappings. (Default without this and driver only is to only map a printer)\r\n \r\n.PARAMETER WriteLog\r\n    Writes Debug data to a log file. (Default is true)\r\n \r\n.PARAMETER RemoveAll\r\n    Removes all mapped printers. (You may need to run this once as the user, and once as an admin for full effect)\r\n \r\n.PARAMETER LocalPrinter\r\n    Specified that the printer is local to the machine. This is needed if mapping a non-network device. (Such as the XPS printer)\r\n \r\n.PARAMTER PrinterOnly\r\n    Only maps a printer, does not add a port, nor install a driver.\r\n \r\n.PARAMETER AdditionalRemovalExclusions\r\n    When used with RemoveAll, prevents the removal of listed printers. By default, Send to One Note, and FAX are excluded.\r\n \r\n.OUTPUTS\r\n    Log file located at C:\\Temp\\.log\r\n \r\n.EXAMPLE\r\n    Install-Printer.ps1 -PrinterCaption \"My Printer\" -DriverName \"Xerox 7845 PS v4\" -PrinterIP \"192.168.1.2\"\r\n \r\n.EXAMPLE\r\n    Install-Printer.ps1 -DriverName \"Xerox 7845 PS v4\" -DriverInf \"\\\\Drivers\\Xerox\\7845\\Driver.inf\" -DriverOnly\r\n \r\n.EXAMPLE\r\n    Install-Printer.ps1 -PrinterCaption \"My Printer\" -Remove -WriteLog:$false\r\n \r\n.EXAMPLE\r\n    Install-Printer.ps1 -PrinterCaption \"My Printer\" -DriverName \"Xerox 7845 PS v4\" -DriverInf \"\\\\Drivers\\Xerox\\7845\\Driver.inf\" -PrinterIP \"192.168.1.2\" -AllSteps\r\n \r\n.EXAMPLE\r\n    Install-Printer.ps1 -RemoveAll -AdditionalRemovalExclusions @(\"My Printer\", \"Send by E-mail printer\")\r\n \r\n.EXAMPLE\r\n    Install-Printer.ps1 -PrinterCaption 'Microsoft XPS Document Writer' -PrinterPortName 'FILE:' -DriverName 'Microsoft XPS Document Writer v4' -PrinterOnly -LocalPrinter\r\n \r\n.NOTES\r\n    Version:        1.3\r\n    Author:         Matthew Thompson\r\n    Creation Date:  2015-12-16\r\n    Purpose\/Change: PrinterOnly, and LocalPrinter Features, Printer Removal by IP, Bug fix in event no printers exist and script is run to install one.\r\n    This script was heavily influenced by Kris Powell. (http:\/\/www.adminarsenal.com\/admin-arsenal-blog\/how-to-add-printers-with-powershell)\r\n#&gt;\r\n[CmdletBinding()]\r\nParam\r\n(\r\n    [string]$DriverName, \r\n    [string]$DriverInf,\r\n    [string]$DriverPath={if ($DriverINF -ne $null){(Split-Path $DriverInf)}}, \r\n    [string]$PrinterIP, \r\n    [int]$PrinterPort=9100, \r\n    [string]$PrinterPortName=(\"IP_\"+$PrinterIP), \r\n    [string]$PrinterCaption, \r\n    [Switch]$Remove,\r\n    [switch]$DriverOnly,\r\n    [switch]$AllSteps,\r\n    [switch]$WriteLog=$true,\r\n    [switch]$RemoveAll,\r\n    [string[]]$AdditionalRemovalExclusions=@(),\r\n    [switch]$LocalPrinter,\r\n    [switch]$PrinterOnly\r\n)\r\n \r\n#Setup the exclusions for RemoveAll\r\n$Exclusions = @(\"Fax\", \"Send To OneNote 2013\", \"Send To OneNote 2010\", \"Microsoft XPS Document Writer\", \"Microsoft XPS Document Writer v4\")\r\n$Exclusions+=$AdditionalRemovalExclusions\r\n \r\n$Log=\"\"\r\n \r\n#Log File Info\r\n$ScriptName = Split-Path -Leaf $PSCommandPath\r\n$LogFile = Join-Path -Path \"C:\\Windows\\Temp\" -ChildPath ($ScriptName+\".log\")\r\n#Do a self-referential lookup to get the script version. As long as the .NOTES section of the file header is up to date and the script is trusted, this should work.\r\n(Get-Help $PSCommandPath -Full -ErrorAction SilentlyContinue).alertset.alert.Text -match \"^\\s*Version\\s*:\\s*[\\w\\d\\.\\,]*\" | Out-Null\r\nif ($matches-ne$null-and$matches.Count-ge1)\r\n{\r\n    $Version = $matches[0] -replace \"^\\s*Version\\s*:\\s*\",\"\"\r\n}\r\n \r\n#Write log data\r\n$Log+=\"Version: \"+$Version+\"`r`n\"\r\n \r\n#Write Parameters to log\r\n$PSBoundParameters.Keys | ForEach-Object -Process {$Log+=(\"`\"\"+$_+\"`\" = `\"\"+$PSBoundParameters[$_]+\"`\"`r`n\")}\r\n \r\n#Creates a printer object for the user.\r\nfunction Create-Printer\r\n{\r\n    Param \r\n    (\r\n        $PrinterCaption, \r\n        $PrinterPortName, \r\n        $DriverName,\r\n        [Switch]$Local=$false\r\n    )\r\n    try\r\n    {\r\n        $Printers = @()\r\n        $Printers += Get-WmiObject -Class \"Win32_Printer\"\r\n        [string]$Log=\"Installed Printers (\"+$Printers.Length.ToString()+\") `r`n\"\r\n        $Printers | ForEach-Object -Process {$Log+=$_.Name+\"`r`n\"}\r\n        if ($Printers.Length -eq 0 -or ($Printers.Name).Contains($PrinterCaption) -eq $false)\r\n        {\r\n            $Log+=\"Attempting to create printer`r`n\"\r\n            $Instance = Set-WmiInstance -Class \"Win32_Printer\" -Arguments @{Caption = $PrinterCaption;DriverName = $DriverName;PortName = $PrinterPortName;DeviceID = $PrinterCaption;Network = (-not $Local)} -ErrorAction Stop\r\n            if ($Instance -ne $null)\r\n            {\r\n                $Log+= \"Printer Created`r`n\"+$Instance.ToString()+\"`r`n\"\r\n            }\r\n            else\r\n            {\r\n                $Log+=\"Install attempted, but printer null`r`n\"\r\n            }\r\n        }\r\n        $Log+= \"Printer Exists: \"+(($Printers.Name).Contains($PrinterCaption)).ToString()+\"`r`n\"\r\n    }\r\n    catch\r\n    {\r\n        $Log+=$_.ToString()+\"`r`n\"\r\n    }\r\n    return $Log\r\n}\r\n \r\n#Installs a driver from an INF file\r\nfunction Install-Driver\r\n{\r\n    Param\r\n    (\r\n        $DriverName, \r\n        $DriverPath, \r\n        $DriverInf\r\n    )\r\n    $Log=\"\"\r\n    try\r\n    {\r\n        $Drivers = @()\r\n        $Drivers += Get-WmiObject -Class \"Win32_PrinterDriver\"\r\n        if ($Drivers.Length -eq 0 -or -not ($Drivers | Where-Object -FilterScript {$_.Name -like ($DriverName+\"*\")}))\r\n        {\r\n            $Instance = ([wmiclass]\"Win32_PrinterDriver\").CreateInstance()\r\n            $Instance.Name = $DriverName\r\n            $Instance.DriverPath = $DriverPath\r\n            $Instance.InfName = $DriverInf\r\n            Invoke-WmiMethod -Class \"Win32_PrinterDriver\" -Name \"AddPrinterDriver\" -ArgumentList @($Instance) -Impersonation Impersonate -EnableAllPrivileges | Out-Null\r\n            $Log+= $Instance.ToString()+\"`r`n\"\r\n        }\r\n        else\r\n        {\r\n            $Log+= (\"Driver Exists: \"+(($Drivers | Where-Object -FilterScript {$_.Name -like ($DriverName+\"*\")})).ToString())+\"`r`n\"\r\n        }\r\n    }\r\n    catch\r\n    {\r\n        $Log+=$_.ToString()+\"`r`n\"\r\n    }\r\n    return $Log\r\n}\r\n \r\n#Creates a printer port required to map a printer\r\nfunction Create-PrinterPort \r\n{\r\n    Param\r\n    (\r\n        $PrinterIP, \r\n        $PrinterPort, \r\n        $PrinterPortName\r\n    )\r\n    $Log=\"\"\r\n    try\r\n    {\r\n        $PrinterPorts = @()\r\n        $PrinterPorts += Get-WmiObject -Class \"Win32_TCPIPPrinterPort\"\r\n        if ($PrinterPorts.Length -eq 0 -or -not ($PrinterPorts.Name).Contains($PrinterPortName))\r\n        {\r\n            $Instance = Set-WmiInstance -Class \"Win32_TCPIPPrinterPort\" -Arguments @{Name = $PrinterPortName;HostAddress = $PrinterIP;PortNumber = $PrinterPort;SNMPEnabled = $false;Protocol = 1}\r\n            $Log+=  $Instance.ToString()+\"`r`n\"\r\n        }\r\n        else\r\n        {\r\n            $Log+= (\"Printer Port Exists: \"+(($PrinterPorts.Name).Contains($PrinterPortName)).ToString())+\"`r`n\"\r\n        }\r\n    }\r\n    catch\r\n    {\r\n        $Log+=$_.ToString()+\"`r`n\"\r\n    }\r\n    return $Log\r\n}\r\n \r\nfunction Get-PrinterPort\r\n{\r\n    Param($IP)\r\n    $Log=\"\"\r\n    $ToReturn = @()\r\n    try\r\n    {\r\n        $PrinterPorts = @()\r\n        $PrinterPorts += Get-WmiObject -Class \"Win32_TCPIPPrinterPort\"\r\n        if ($PrinterPorts.Length -eq 0 -or -not ($PrinterPorts.HostAddress).Contains($IP))\r\n        {\r\n            #$Instance = Set-WmiInstance -Class \"Win32_TCPIPPrinterPort\" -Arguments @{Name = $PrinterPortName;HostAddress = $PrinterIP;PortNumber = $PrinterPort;SNMPEnabled = $false;Protocol = 1}\r\n            $Log+= \"No Printer Port exists matching $IP\"\r\n        }\r\n        else\r\n        {\r\n            $ToReturn = $PrinterPorts | Where-Object -FilterScript {$_.HostAddress -eq $IP}\r\n            $Log+= (\"Printer Port Exists: \"+(($PrinterPorts.Name).Contains($PrinterPortName)).ToString())+\"`r`n\"\r\n        }\r\n    }\r\n    catch\r\n    {\r\n        $Log+=$_.ToString()+\"`r`n\"\r\n    }\r\n    return $ToReturn\r\n}\r\n \r\n#Deletes an already mapped printer\r\nfunction Delete-Printer\r\n{\r\n    Param($PrinterName, $IP)\r\n    $Log=\"\"\r\n    try\r\n    {\r\n        $Printers = @()\r\n        $Printers += Get-WmiObject -Class \"Win32_Printer\"\r\n        if ($PrinterName -ne $null)\r\n        {\r\n            $Printers = $Printers | Where-Object -FilterScript {$_.Name -eq $PrinterName}\r\n        }\r\n        elseif ($IP -ne $null)\r\n        {\r\n            $Ports = Get-PrinterPort -IP $IP\r\n            $NewPrinterList = @()\r\n            foreach ($Port in $Ports)\r\n            {\r\n                $NewPrinterList += $Printers | Where-Object -FilterScript {$_.PortName -eq $Port.Name}\r\n            }\r\n            $Printers = $NewPrinterList\r\n        }\r\n        else\r\n        {\r\n            $Log+=\"No parameters specified to limit removal. No printers removed\"\r\n            return $Log\r\n        }\r\n        foreach($Printer in $Printers)\r\n        {\r\n            $Printer.Delete() | Out-Null\r\n            $Log+= $Printer.Name+\" deleted`r`n\"\r\n        }\r\n    }\r\n    catch\r\n    {\r\n        $Log+=$_.ToString()+\"`r`n\"\r\n    }\r\n    return $Log\r\n}\r\n \r\n#Script Entry\r\nif ($Remove)\r\n{\r\n    Write-Host \"Removing $PrinterCaption\"\r\n    #Remove a printer selected\r\n    $Log+=Delete-Printer -PrinterName $PrinterCaption -IP $PrinterIP\r\n}\r\nelseif($RemoveAll)\r\n{\r\n    #Remove most printers\r\n    $Printers = @()\r\n    $Printers += Get-WmiObject -Class \"Win32_Printer\" | Where-Object -FilterScript {$Exclusions.Contains($_.Name) -eq $false}\r\n    if ($Printers.Length -gt 0)\r\n    {\r\n        Write-Host (\"Attempting to remove \"+$Printers.Length+\" printers. You may need to run this script once as admin, and once as the user for all printers to be removed.\")\r\n        foreach($Printer in $Printers)\r\n        {\r\n            $Log+=(Delete-Printer -PrinterName $Printer.Name)+\"`r`n\"\r\n        }\r\n    }\r\n    else\r\n    {\r\n        Write-Host \"No printers require removal.\"\r\n    }\r\n}\r\nelse\r\n{\r\n    #Install a printer\r\n    if ($DriverOnly -or $AllSteps)\r\n    {\r\n        #Install the printer Driver\r\n        Write-Host \"Installing $DriverName\"\r\n        $Log+=(Install-Driver -DriverName $DriverName -DriverPath $DriverPath -DriverInf $DriverInf)+\"`r`n\"\r\n    }\r\n    if (-not $DriverOnly -and -not $PrinterOnly)\r\n    {\r\n        #Output for user visibility\r\n        Write-Host \"Mapping printer ($PrinterCaption) from IP $PrinterIP\"\r\n        #Add the printer port\r\n        $Log+=(Create-PrinterPort -PrinterIP $PrinterIP -PrinterPort $PrinterPort -PrinterPortName $PrinterPortName)+\"`r`n\"\r\n        #Finally, create the printer\r\n        $Log+=(Create-Printer -PrinterCaption $PrinterCaption -PrinterPortName $PrinterPortName -DriverName $DriverName -Local $LocalPrinter)+\"`r`n\"\r\n    }\r\n    if ($PrinterOnly)\r\n    {\r\n        #Output for user visibility\r\n        Write-Host \"Mapping printer ($PrinterCaption) from port $PrinterPortName\"\r\n \r\n        $Log+=(Create-Printer -PrinterCaption $PrinterCaption -PrinterPortName $PrinterPortName -DriverName $DriverName -Local $LocalPrinter)+\"`r`n\"\r\n    }\r\n}\r\n#Write the log to a file\r\nif ($WriteLog)\r\n{\r\n    $Log | Out-file $LogFile\r\n    Write-Verbose $Log\r\n}<\/code><\/pre>\n<h3>Driver Detection<\/h3>\n<p>Edit the &#8220;lt;DriverName&gt;&#8221; Portion of the script below with the name of your driver. Then add the script into SCCM as your script-based detection method for the INF driver installation.<\/p>\n<pre><code>#Note that this script is not perfect, you may need to change the detection method if you have multiple similiarly named drivers.\r\n$DriverName = \"\" #Replace this with your driver name\r\n$Drivers = Get-WmiObject -Class \"Win32_PrinterDriver\"\r\nif (($Drivers | Where-Object -FilterScript {$_.Name -like ($DriverName+\"*\")}))\r\n{\r\n    Write-Host \"Driver already installed\"\r\n}\r\nexit 0<\/code><\/pre>\n<h3>Printer Detection<\/h3>\n<p>Edit the &#8220;&lt;PrinterName&gt;&#8221; portion of the script below with your own printer name. Then add this to your printer deployment as the script-based detection method.<\/p>\n<pre><code>$PrinterCaption = \"\" #Change this to your printer name\r\n$Printers = Get-WmiObject -Class \"Win32_Printer\"\r\nif (($Printers.Name).Contains($PrinterCaption))\r\n{\r\n    Write-Host \"Printer already installed\"\r\n}\r\nexit 0<\/code><\/pre>\n<h3>Remove-Printer (Interactive Printer Removal Tool)<\/h3>\n<p>This must be placed in the same directory as Install-Printer.ps1 This will present a user with a list of printers to exclude. The user can then select a printer by inputting it&#8217;s number. If they do, it will be excluded. Once finished, the script will remove all other printers.<\/p>\n<pre><code>&lt;# .SYNOPSIS Interactively assists a user in removing printers .DESCRIPTION Interactively assists a user in removing printers .NOTES Version: 1.0 Author: Matthew Thompson Creation Date: 2017-01-08 Purpose\/Change: Updated for Blog #&gt;\r\n \r\n#Printers to exclude by default\r\n$Exclusions = @(\"Fax\", \"Send To OneNote 2013\", \"Send To OneNote 2010\")\r\n \r\n#Grab a list of printers\r\n$Printers = Get-WmiObject -Class \"Win32_Printer\" | Where-Object -FilterScript {$Exclusions.Contains($_.Name) -eq $false}\r\n \r\n#Stores additional exclusions by user\r\n$AdditionalExclusions=@()\r\n \r\n#Infinite loop (Breaks out with user interaction)\r\nwhile($true)\r\n{\r\n    Write-Host \"Printers to remove:\"\r\n    #Print all non excluded printers for user to browse\r\n    $NonExcluded=@()\r\n    $NonExcluded += $Printers | Where-Object -FilterScript {$AdditionalExclusions.Contains($_.Name) -eq $false}\r\n    for($I=0;$I -lt $NonExcluded.Length;$I++)\r\n    {\r\n        Write-Host ($I.ToString()+\") \"+$NonExcluded[$I].Name)\r\n    }\r\n    #Ask user to exlude another printer or continue\r\n    $UInput = Read-Host -Prompt \"Please select a printer to exclude or type `\"f`\" to finish\"\r\n    if ($UInput.ToLower() -eq \"f\")\r\n    {\r\n        #On continue, break out of infinite loop\r\n        break;\r\n    }\r\n    #Otherwise matching selected index to printer, and add.\r\n    $UIndex=$null\r\n    if ([int]::TryParse($UInput, [ref]$UIndex) -and $UIndex -ge 0 -and $UIndex -lt $NonExcluded.Length)\r\n    {\r\n        $AdditionalExclusions+=$NonExcluded[$UIndex].Name\r\n    }\r\n    else\r\n    {\r\n        #In case someone can't type a number\r\n        Write-Host \"Input could not be parsed, please try again\"\r\n    }\r\n}\r\n#Call the parent script with the selected exclusions\r\nWrite-Host \"Removing printers\"\r\n&amp;\".\\Install-Printer.ps1\" -RemoveAll -AdditionalRemovalExclusions $AdditionalExclusions -WriteLog<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Code Release This script is intended to assist in SCCM deployments of optional printers. The intended effect is to allow a user to open Software Center or the SCCM Application Catalog and click an option such as &#8220;Map me to XXX Printer&#8221;, and have the printer, port, and driver all install automatically. Parameters DriverName &lt;Object&gt; &hellip; <a href=\"https:\/\/www.ziviz.net\/WP\/2017\/06\/18\/install-printer\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Install-Printer&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4,8,7],"tags":[],"class_list":["post-67","post","type-post","status-publish","format-standard","hentry","category-code-release","category-powershell","category-sccm","category-sysadmin"],"_links":{"self":[{"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/posts\/67","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/comments?post=67"}],"version-history":[{"count":5,"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/posts\/67\/revisions"}],"predecessor-version":[{"id":81,"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/posts\/67\/revisions\/81"}],"wp:attachment":[{"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/media?parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/categories?post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/tags?post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}