No announcement yet.

powershell script to isntall printers

  • Filter
  • Time
  • Show
Clear All
new posts

  • powershell script to isntall printers

    hi all,

    I have a Powershell script that we just wrote that install printers based on the location properties of the computer account in AD.
    the problem is that I am using an AD cmdlet and there for it will not run on the local PCS.

    We have about 50 printers and about 1000 pcs very little xp majority win 7 and some win 8 and about 40 locations, the idea is that installing printers via GPO takes at time about 10min. using the powershell will reduce it to about 2 min.

    If anyone has this problem of can contribute that will be great both me and my friend Tim are new to powershell ( and love it: )

    $localcomputer = hostname
    $localcomputer = "dtwc1000"
    [Microsoft.ActiveDirectory.Management.ADComputer] $henry = Get-ADComputer -Identity $localcomputer -Properties location
    function AddPrinter {
        if($location -eq "$$$$$$"){
            Write-Host "   \\\printservername\shareprintername"; Add-Printer -ConnectionName "\\printservername\shareprintername"
        if($location -eq "%%%%%"){
            Add-Printer -ConnectionName "\\\printservername\shareprintername"
    if($str.Trim().Length -le 0){
        #Default Case if no location set
        Write-Host "Default:"
        #Write-Host "      \\printservername\shareprintername"; Add-Printer -connectionName "\\printservername\shareprintername"
    foreach($location in $locations){
        if($location.Length -gt 0){
            $campus=$tokens[0].Trim() +"-" +$tokens[1].Trim()
            AddPrinter $campus
    Last edited by Dumber; 16th October 2014, 10:47. Reason: code tags

  • #2
    Re: powershell script to isntall printers

    How is this script being run? Is it running on the client computer somehow? The Add-Printer cmdlet requires PowerShell v3 or later which. Is the only issue the AD piece?

    Get-ADComputer is part of the ActiveDirectory module which I doubt you have installed on clients. You can fall back to use ADSI or the .NET AD classes like the DirectorySearcher.

    This is code that can run ON the client computer.

    $searcher=New-Object DirectoryServices.DirectorySearcher
    $searcher.Filter="(&(objectcategory=computer)(name =$env:computername))"
    $me = $searcher.FindOne()

    Last edited by Jeff Hicks; 15th October 2014, 13:43.


    • #3
      Re: powershell script to isntall printers

      Hi Jeff,

      Thank you very much.
      I did a small change to the script (too late to remember I have it at work) I will post it tomorrow.

      But you did help a lot Really appreciate your quick reply.


      • #4
        Re: powershell script to isntall printers

        hi ED,
        Sorry for the late reply had to do other urgent work.

        The add-printer is causing us problems. not sure what is going on.
        we installed powershell v3 and tested but it didnt work for us.

        We also have a number of windows 8 PCs where the add-printer and remove-printer dont seems to function as they should.

        My friend Tim who has a great knowledge of programming wrote a C# script to set to allocate the printer to the PC using the location of the computer.

        We used powershell to update the location properties on the computer accounts.

        still facing some major issue with powershell and printer.

        I am opening another threat .


        • #5
          Re: powershell script to isntall printers

          What do you mean PowerShell 3 "didn't work for you?" Does this mean you have older clients that don't support v3?


          • #6
            Re: powershell script to isntall printers

            Hi Jeff,

            The PCs that I am testing it on have PowerShell V3.


            • #7
              Re: powershell script to isntall printers

              But I still don't know what you mean when you say "it didn't work for you".


              • #8
                Re: powershell script to isntall printers

                Sorry Jeff I feel the pressure from life

                ok the use of Get-printer or Add-printer don't seems to work on my PC on the network.
                The Get-Printer I understand that it depends on who installed the printers and who run the scripts so if the installation is done by a different user the get-printer will not show those printers.

                I manage to resolve this by using the win32 and get the printers objects that has the properties network and delete those printers - I saw this script before on the net :
                "Function Delete-networkPrinters {

                $netprinters = Get-WmiObject -Class win32_Printer | where-object{$}
                If ($netprinters -ne $null)

                Foreach($netprinter in $netprinters)

                Write-Host "The following printers were removed:" + $ -ForegroundColor green

                Else {

                Write-Warning "Cannot find network printer in the currently environment."



                the add-printer is not working no matter what I do with the add printer. I am checking this