title: Adding Shared Printer to Remote Computer via Powershell
body: <p>I am unable to run an invoke-command script to install printers on a remote machine.  My code works locally, but as soon as I pipe it into Invoke-command, i get errors.</p>
  
  <p>Local:</p>
  
  <pre><code>$Printer = "\\server1\printer1"
  (New-Object -Com Wscript.Network).AddWindowsPrinterConnection($Printer)
  </code></pre>
  
  <p>And this adds the printer just fine.  I can do this same command on the remote computer with no issues.  But when i tried to execute the command remotely I have the issues.</p>
  
  <p>Remote:</p>
  
  <pre><code>$compname = "computer"
  $Printer = "\\server1\printer1"
  Invoke-Command -ComputerName $CompName -Scriptblock {(New-Object -Com Wscript.Network).AddWindowsPrinterConnection('$Printer')}
  </code></pre>
  
  <p>Which returns the error "The printer name is invalid"</p>
  
  <p>So I tried to see what the shell was sending to the remote computer with the following code, and everything in the write output looks good, but I still get errors:</p>
  
  <pre><code>Invoke-Command -ComputerName $CompName -Scriptblock {(New-Object -Com Wscript.Network).AddWindowsPrinterConnection('$Printer'); write-host "(New-Object -Com Wscript.Network).AddWindowsPrinterConnection('$Printer')"}
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Exception calling "AddWindowsPrinterConnection" with "1" argument(s): "The printer name is invalid. (Exception from
  HRESULT: 0x80070709)"
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : ComMethodTargetInvocation
      + PSComputerName        : computer
  
  (New-Object -Com Wscript.Network).AddWindowsPrinterConnection('\\server1\printer1')
  </code></pre>
  
  <h1>Edit 1/5/2015</h1>
  
  <p>So I've tried Paul's code with a number of different entries to the argumentlist.  All have not worked so far.  i think the first 3 are closer to an answer.</p>
  
  <pre><code>-ArgumentList ""\\server1\printer1""
  -ArgumentList ""'\\server1\printer1'""
  -ArgumentList "\"\\server1\printer1""
  </code></pre>
  
  <p>Results in:</p>
  
  <pre><code>Invoke-Command : A positional parameter cannot be found that accepts argument '\\server1\printer1'.
  At line:1 char:1
  + Invoke-Command -ComputerName $CompName -Scriptblock {(New-Object -Com Wscript.Ne ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
      + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand
  
  
  
  -ArgumentList "'\\server1\printer1'"
  -ArgumentList \'"\\server1\printer1"'
  -ArgumentList \""\\server1\printer1""
  -ArgumentList \"\\server1\printer1"
  </code></pre>
  
  <p>Result in:</p>
  
  <pre><code>Exception calling "AddWindowsPrinterConnection" with "1" argument(s): "The printer name is invalid. (Exception from
  HRESULT: 0x80070709)"
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : ComMethodTargetInvocation
      + PSComputerName        : sso-mxl327082y
  </code></pre>