public

Setting the default printer based on network subnet.

  • Download Gist
Set-PrinterByLocation.ps1
PowerShell
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
$homeRange = "10.1.1.*"
$remoteRange = "10.7.104.*"
$remotePrinter = "W3Printer_01"
$homePrinter = "AdminPrinter"
 
function Set-DefaultPrinter([string]$printerPath) {
$printers = gwmi -class Win32_Printer -computer .
Write-Host -fore cyan "Default Printer: $printerPath"
$dp = $printers | ? { $_.deviceID -match $printerPath }
$dp.SetDefaultPrinter() | Out-Null
}
 
$ipAddress = @()
$ipAddress = gwmi win32_NetworkAdapterConfiguration |
? { $_.IPEnabled -eq $true } |
% { $_.IPAddress } |
% { [IPAddress]$_ } |
? { $_.AddressFamily -eq 'internetwork' } |
% { $_.IPAddressToString }
 
Write-Host -fore cyan "Your current network is $ipAddress."
 
switch -wildcard ($ipAddress) {
$homeRange { Set-DefaultPrinter $homePrinter }
$remoteRange { Set-DefaultPrinter $remotePrinter }
default { Set-DefaultPrinter $homePrinter }
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.