title: PowerShell UnauthorizedAccessException writing file on network share
body: <p>I'm trying to write a PowerShell script which minifies a directory full of .js files on the webserver. The files belong to me; I created it; I'm logged in as me. I can open and save the files in a text editor or via a batch file. The PowerShell script thinks it's operating as me (<code>$env:username</code>), and I'm running it from a PowerShell console that I ran with "Run As Administrator". </p>
  
  <p>The loop looks like this. $files is confirmed to be a collection of file objects representing the files I want. </p>
  
  <pre><code>$tool = "c:\Program Files (x86)\Microsoft\Microsoft Ajax Minifier\ajaxmin.exe";
  
  foreach($ofile in $files) {
      $outfile = $ofile.DirectoryName + "\" + $ofile.BaseName + ".min.js";
  
      &amp; $tool $ofile.FullName &gt; $outfile;
  }
  </code></pre>
  
  <p>I get the following error on each file: </p>
  
  <pre><code>Access to the path '\\webserver\inetpub\whatever\js\validation.min.js' is denied.
  At C:\minifyall.ps1:18 char:27
  +     &amp; $tool $ofile.FullName &gt; &lt;&lt;&lt;&lt;  $outfile;
      + CategoryInfo          : OpenError: (:) [], UnauthorizedAccessException
      + FullyQualifiedErrorId : FileOpenFailure
  </code></pre>
  
  <p>The file there is the file it ought to be writing to, and get-acl tells me I have Allow FullControl. It can read the non-.min files just fine. </p>
  
  <p>If I change the output directory to the current working directory (on my own machine), the script works as expected:</p>
  
  <pre><code># No prob
  $outfile = $ofile.BaseName + ".min.js";
  </code></pre>
  
  <p>Is there something else I need to do to enable this script to write to remote files? Other than rewriting it in Perl (Update: In the end, I rewrote it in Perl and as of February 2020 have had no trouble with it. <strong>This is not an endorsement of Perl</strong>). </p>
  
  <p>Tips on PowerShell coding style are welcome too. My professional experience with PowerShell began approximately an hour after breakfast today. </p>