13 March 2014

Get list of installed printers

Posted By: Mick Pletcher - 10:50 AM

Share

& Comment











This function will get the list of printers installed for a user. It will also get the Default Printer. It outputs the list to Printers.txt, located at the location where the script is executed.

You can download the script here.


Function GetRelativePath {
    $Global:RelativePath=(split-path $SCRIPT:MyInvocation.MyCommand.Path -parent)+"\"
}

Function GetPrinterList {

    #Declare Local Memory
    Set-Variable -Name Count -Scope Local -Force
    Set-Variable -Name DefaultPrinter -Scope Local -Force
    Set-Variable -Name Printers -Scope Local -Force
    Set-Variable -Name Temp -Scope Local -Force
    
    If (Test-Path -Path $Global:RelativePath"Printers.txt") {
        Remove-Item -Path $Global:RelativePath"Printers.txt" -Force
    }
    $DefaultPrinter = Get-WmiObject -Query " SELECT * FROM Win32_Printer WHERE Default=$true"
    $DefaultPrinter = $DefaultPrinter.ToString()
    $DefaultPrinter = $DefaultPrinter -replace [char]34,""
    $DefaultPrinter = $DefaultPrinter -replace "\\\\","\"
    $DefaultPrinter = $DefaultPrinter.split("=")
    $Temp = "Default Printer: "+$DefaultPrinter[$DefaultPrinter.Length-1]
    $Temp | Add-Content -Path $Global:RelativePath"Printers.txt"
    $Printers = Get-WmiObject -Query " SELECT * FROM Win32_Printer WHERE Default=$false"
    For ($Count=0; $Count -lt $Printers.Length; $Count++) {
        $Temp = $Printers[$Count]
        $Temp = $Temp.ToString()
        $Temp = $Temp -replace [char]34,""
        $Temp = $Temp -replace "\\\\","\"
        $Temp = $Temp.split("=")
        $Temp = "Printer: "+$Temp[1]
        $Temp | Add-Content -Path $Global:RelativePath"Printers.txt"
        Write-Host $Temp
    }
    
    #Cleanup Local Memory
    Remove-Variable -Name Count -Scope Local -Force
    Remove-Variable -Name DefaultPrinter -Scope Local -Force
    Remove-Variable -Name Printers -Scope Local -Force
    Remove-Variable -Name Temp -Scope Local -Force
    
}

About Mick Pletcher

My blog is here to help solve issues I have encountered and solved, publish scripts I have written, and educate others in understanding areas that are not well covered

0 comments:

Post a Comment

Copyright © 2013 Mick's IT Blogs™ is a registered trademark.