Repository navigation
Expand file tree
/
Copy pathRemove-NetworkPrinter.ps1
More file actions
57 lines (47 loc) · 1.69 KB
/
Copy pathRemove-NetworkPrinter.ps1
File metadata and controls
57 lines (47 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<#
.SYNOPSIS
Removes a network printer based on the provided IP address or printer name.
.DESCRIPTION
This function removes a network printer based on the input provided. It can remove a printer using either its IP address or printer name.
.PARAMETER printer
Specifies the IP address or printer name to remove.
.EXAMPLE
Remove-NetworkPrinter -printer "192.168.1.1"
Removes the network printer with the IP address 192.168.1.1.
.EXAMPLE
Remove-NetworkPrinter -printer "PrinterName"
Removes the network printer with the name "PrinterName."
.NOTES
Author: Smitty
Date: 10/3/2023
Version: 1.0
#>
function Remove-NetworkPrinter {
param (
[Parameter()]
[string]$printer
)
switch -Regex ($printer) {
'\b(?<ipv4>\d{1,3}(?:\.\d{1,3}){3})\b' {
if ($Matches['ipv4'] -as [IPAddress]) {
# It's an IP address - Removing Printer by IP Address
$printerToRemove = Get-WmiObject -Class Win32_Printer | Where-Object { $_.PortName -eq $printer }
Write-Host "Removing Printer by IP Address"
}
}
default {
# This is the default case for printer name processing
$printerToRemove = Get-WmiObject -Class Win32_Printer | Where-Object { $_.ShareName -eq $printer }
Write-Host "Its not an IP Address - Removing by Printer Name"
}
}
if ($printerToRemove) {
#Removes the printer
$printerToRemove.Delete()
Write-Host "Printer removed successfully."
}
else {
Write-Host "Printer not found or removal failed."
}
}
Remove-NetworkPrinter -printer "Printer1"