title: powershell - creating dynamic printer script - output and command stucture
body: <p>I am attempting to create a script using powershell that I can use to make a list of:</p>
  
  <ul>
  <li>network printers a user currently has installed</li>
  <li>printer drivers that are currently installed on the users PC</li>
  </ul>
  
  <p>I have managed to come up with the following script. I am still very new to powershell and I know it isn't the prettiest, but it is working to some degree.</p>
  
  <pre><code>$username=[Environment]::UserName
  $machinename=[Environment]::MachineName
  
  ## Get installed printers and create template command
  $printers=get-WmiObject Win32_printer -Property Name | where { $_.Name -like "\\printserver1\*" -or $_.Name -like "\\printserver2\*" }
  
  $printers2=$printers | select-object Name
  $printers2 &gt; C:\testdir\"$username"PRINTERS.txt
  
  $printers3=get-content C:\testdir\"$username"PRINTERS.txt | select-string -pattern "\\"
  
  
  foreach ( $y in $printers3 )
   {
   write-host RUNDLL32 PRINTUI.DLL','PrintUIEntry /dn /n'"'$y
   } 
  
      ## Get drivers and create template command
  
  $printerdrivers=get-wmiobject -class Win32_PrinterDriver -Property Name
  $printerdrivers2=$printerdrivers -split('=') #| select-string 'Name'
  $printerdrivers3=$printerdrivers2 | select-string 'HP'
  
  foreach ( $i in $printerdrivers3 )
    {
     $removeprinter=$i -split (',') | select-string 'HP'
     #write-host $removeprinter
     write-host rundll32 printui.dll','PrintUIEntry /dd /c\\$machinename /m $removeprinter'"' /h "x64" /v "Type 3 - User Mode"
    }
  </code></pre>
  
  <p>the above code produces the following output: </p>
  
  <pre><code>    RUNDLL32 PRINTUI.DLL,PrintUIEntry /dn /n"\\printserver1\ACC_PRINT_HP9040UPS                                                                                                                                                                      
  
  rundll32 printui.dll,PrintUIEntry /dd /c\\machine1 /m "HP Universal Printing PS (v5.1)" /h x64 /v Type 3 - User Mode
  rundll32 printui.dll,PrintUIEntry /dd /c\\machine1 /m "HP Universal Printing PS (v5.2)" /h x64 /v Type 3 - User Mode
  rundll32 printui.dll,PrintUIEntry /dd /c\\machine1 /m "HP Designjet T770 24in HPGL2" /h x64 /v Type 3 - User Mode
  </code></pre>
  
  <p>where I am having problems is in two places.</p>
  
  <p>Firstly, is it possible for me to run these commands from within the same script without outputting them to a text file and running them as a batch file? When I attempting to run the template commands I keep getting an error saying that the printer doesn't exist. Unfortunately I can't tell how powershell is actually formulating the command.</p>
  
  <p>Secondly, for the printers installed section, I can't get quotation marks around the printer name for some reason when I do the write-host command. I can get it to show up at the beginning of the printer name "<strong>"\printerserver1\ACC_PRINT_HP9040UPS</strong>" , but not at the end.</p>
  
  <p>I know this code is probably really bad, but I am hoping that I have something here that I can work with. My primary goal was to have something dynamic that I can run against any computer and have it clean up the printers and printer drivers. I will be running it in conjunction with our regular batch files. That is where I will be controlling the 'spooler' service.</p>
  
  <p>Any help or direction would be greatly appreciated!</p>
  
  <p>Cheers</p>
  
  <p>===================== Edit section to contain changes empo suggested!
  I just wanted to add as well that the function "Remove-Spaces" is not my work!
  I found it on another website and incorporated it into my script. <a href="http://www.dougfinke.com/blog/index.php/2009/03/02/powershell-remove-spaces/" rel="nofollow">Original is here</a></p>
  
  <pre><code>   $username=[Environment]::UserName
      $machinename=[Environment]::MachineName
      ## this function removes all the blank spaces I had
      Function Remove-Spaces {
        param($target)
  
        begin {
          filter Do-RemoveSpaces { $_ -replace "\s *", "" }
        }
  
        process { if($_) { $_ | Do-RemoveSpaces } }
  
        end { if($target) {$target | Do-RemoveSpaces} }
      }
  
  
      $printers=get-WmiObject Win32_printer -Property Name | where { $_.Name -like "\\printerserver1\*" -or $_.Name -like "\\printerserver2\*" }
      $printers2=$printers | select-object Name | select-string -pattern "\\"
      $printers2 &gt; C:\testdir\"$username"PRINTERS.txt
      $printers3=get-content C:\testdir\"$username"PRINTERS.txt | select-string -pattern "\\"
  
      foreach ( $y in Remove-Spaces $printers3 )
       {
        write-output "RUNDLL32 PRINTUI.DLL, PrintUIEntry /dn /n`"$y`"" | out-file *blah*
       }
  
      $printerdrivers=get-wmiobject -class Win32_PrinterDriver -Property Name
      $printerdrivers2=$printerdrivers -split('=') #| select-string 'Name'
      $printerdrivers3=$printerdrivers2 | select-string 'HP'
  
      foreach ( $i in $printerdrivers3 )
        {
         $removeprinter=$i -split (',') | select-string 'HP'
         #write-host $removeprinter
         write-output "rundll32 printui.dll, PrintUIEntry /dd /c\\$machinename /m $removeprinter`"` /h x64 /v `"Type 3 - User Mode`"" | out-file *blah*
        }
  </code></pre>
  
  <p>The output now looks like this:</p>
  
  <pre><code>RUNDLL32 PRINTUI.DLL, PrintUIEntry /dn /n"\\printerserver1\ACC_PRINT_HP9040UPS"
  rundll32 printui.dll,PrintUIEntry /dd /c\\machine1 /m "HP Universal Printing PS (v5.1)" /h x64 /v Type 3 - User Mode
  rundll32 printui.dll,PrintUIEntry /dd /c\\machine1 /m "HP Universal Printing PS (v5.2)" /h x64 /v Type 3 - User Mode
  rundll32 printui.dll,PrintUIEntry /dd /c\\machine1 /m "HP Designjet T770 24in HPGL2" /h x64 /v Type 3 - User Mode
  </code></pre>
  
  <p>I believe that this will work... hopefully anyway. I am going to do some testing and I will report back and make sure to mark this as answered!</p>
  
  <p>Scratch the out-file fix.. it doesn't work :(
  still need to do additional testing</p>
  
  <p>thank you!</p>