-
Notifications
You must be signed in to change notification settings - Fork 0
/
Install-ADPrinters.ps1
73 lines (60 loc) · 2.78 KB
/
Install-ADPrinters.ps1
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#requires -version 2.0
<#
.SYNOPSIS
Install printers from active directory.
.DESCRIPTION
The PowerShell script which can be used to set the default printer.
.EXAMPLE
C:\PS> C:\Script\Install-ADPrinters.ps1
This command shows how to set install printers from Active Directory.
#>
# Get rid of existing printers
Get-WMIObject Win32_Printer | foreach{$_.delete()}
# Get the OU for this computer
$ComputerOU = Get-ADOrganizationalUnit -Identity $(($adComputer = Get-ADComputer -Identity $env:COMPUTERNAME).DistinguishedName.SubString($adComputer.DistinguishedName.IndexOf("OU=")))
# Connect to active directory
$root = New-Object system.DirectoryServices.DirectoryEntry
# Search active directory for printers
$search = New-Object system.DirectoryServices.DirectorySearcher($root)
$search.PageSize = 1000
$search.Filter = "(objectCategory=printQueue)"
$search.SearchScope = "Subtree"
$search.SearchRoot = $root
# Get the details of each printer
$colProplist = "Name","UNCName","PortName","PrintShareName","ShortServerName"
foreach ($i in $colProplist){$search.PropertiesToLoad.Add($i) | out-null }
$RawADPrinters = $search.FindAll()
# List of unique print servers
$PrintServers = @()
foreach ($RawADPrinter in $RawADPrinters) {
if ($PrintServers -notcontains $RawADPrinter.Properties.shortservername) {
$PrintServers += $RawADPrinter.Properties.shortservername
}
}
# List of unique printers
$ADPrinters = @()
foreach ($PrintServer in $PrintServers) {
foreach ($Printer in Get-Printer -Computer $PrintServer | ? Shared -eq $True) {
if ($ADPrinters -notcontains $Printer) {
$ADPrinters += $Printer
}
}
}
# For each printer
foreach ($Printer in $ADPrinters) {
# Search the printer's comment for the OU of this printer, with an asterisk on the end
if ($Printer.Comment.Split("|") -Contains $ComputerOU+"*") {
# Found it, installing printer on this PC
Add-Printer -ConnectionName ("\\"+$Printer.ComputerName+"\"+$Printer.Name)
# The asterisk is used to signify that this should be the default printer for computers in this OU
# There's no direct PowerShell method for setting the default printer, so we have to do this through WMI
# Get a WMI Instance of the printer we just installed
$WMIPrinter = Get-WmiObject -Class Win32_Printer | Where{$_.Name -eq ("\\"+$Printer.ComputerName+"\"+$Printer.Name)}
# Set it as default
$WMIPrinter.SetDefaultPrinter()
# Search the printer's comment for the OU of this printer, without an asterisk at the end
} else if ($Printer.Comment.Split("|") -Contains $ComputerOU) {
# Found it, installing printer on this PC
Add-Printer -ConnectionName ("\\"+$Printer.ComputerName+"\"+$Printer.Name)
}
}