13 March 2014

Get list of installed printers

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
    
}

Related Posts:

  • List all Local Administrators on a Computer I wrote this script to generate a list of local administrators on a PC. It saves the output to a text file at a central repository. The text file is named the computer name and contains a listing of all the local administr… Read More
  • Replicate Permissioning Here is a script I have written that will replicate the permissions between two folders including all subfolders and file permissions. Execute the script and you will be prompted for the source and destination folders. It w… Read More
  • Uninstall All Printers Recently, we upgraded our print servers and needed to reinstall all of the printers. This script will uninstall all printers. I deployed this script out and had it run as the user and a GPO reinstalled the printer with the … Read More
  • Cleaning up old systems in Active Directory, SCCM, and Antivirus Every place I have worked, there has been the issue of systems being in SCCM, AD, and antivirus that no longer existed. The is often caused by systems being overlooked when a user departs the company, a laptop that gets put… Read More
  • Import and Apply Local GPOs This script will import and apply a local GPO using the local GPO utility, ImportRegPol.exe, located here. The script is a wrapper that makes implementing this utility a snap. All that has to be done is to use the Micr… Read More

0 comments:

Post a Comment