forked from SamErde/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet-DefaultPrinter.ps1
More file actions
57 lines (53 loc) · 2.33 KB
/
Copy pathSet-DefaultPrinter.ps1
File metadata and controls
57 lines (53 loc) · 2.33 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
function Set-DefaultPrinter {
<#
.SYNOPSIS
Set the user's default printer in Windows.
.DESCRIPTION
Set the user's default printer in Windows. Shows a list of installed printers if none is specified.
.PARAMETER PrinterName
The name of an installed printer to set as the default.
Includes tab-autocomplete to select from the currently installed printers.
If no printer name is specified, a list of installed printers will be shown without making a change.
.NOTES
Author: Sam Erde
Date Modified: 2024-06-11
Version: 0.1.1
Dedicated to Matt Dillon, our Intune Engineer Extraordinaire!
#>
[CmdletBinding()]
param (
# Name of the printer to set as your default. Provides tab-autocomplete with names of installed printers.
# The way this handles printer names with spaces in them feels sloppy, but works. Got any better ideas for me?
[Parameter()]
[ArgumentCompleter( {
param ( $CommandName, $ParameterName, $WordToComplete, $CommandAst, $FakeBoundParameters )
[array]$Script:PrinterNames = "'$((Get-Printer).Name -join "','")'" -split ','
$Script:PrinterNames
}
)]
[ValidateScript({
if ("'" + $_ + "'" -in $Script:PrinterNames) {
$true
}
else {
throw "`n$_ is not a valid printer name. Please use one of the following: $($Script:PrinterNames -join ', ')"
}
})]
[string]
$PrinterName
)
if (-not $PSBoundParameters.ContainsValue($PrinterName)) {
Get-Printer
Write-Host "`nNo printer name was specified, so here is a list of installed printers." -ForegroundColor Green -BackgroundColor Black
return
}
$UnquotedPrinter = $PrinterName -replace "'",""
$Printer = Get-CimInstance -Class Win32_Printer -Filter "Name=`'$UnquotedPrinter`'"
# SetDefaultPrinter if the specified printer name is found
if ($Printer) {
Invoke-CimMethod -InputObject $Printer -MethodName SetDefaultPrinter
}
else {
Write-Host "No printer with the name '$UnquotedPrinter' was found." -ForegroundColor Yellow -BackgroundColor Black
}
}