title: Powershell copy a file or get the content without lock it
body: <p>We have some scheduled scripts. They have to access a file "Functions.ps1" with dot sourced functions. This "Functions.ps1" is located on a share. Because the ExecutionPolicy I can't load the file like this:</p>
  
  <pre><code>. \\share\folder\Functions.ps1
  </code></pre>
  
  <p>The scheduled script must do a copy to the local C:\ and load the file:</p>
  
  <pre><code>$str_ScriptPath = "$($env:LOCALAPPDATA)\$(Get-Random -Minimum 0 -Maximum 100000)_Functions.ps1"
  Copy-Item -Path "$str_Share\Functions.ps1" -Destination $str_ScriptPath -Force   
  . $str_ScriptPath
  Remove-Item -Path $str_ScriptPath 
  </code></pre>
  
  <p>The problem is that some scripts are scheduled at the same time.<br>
  In this case it occures an error:</p>
  
  <pre><code>The process cannot access the file 'C:\Windows\system32\config\systemprofile\AppData\Local\88658_Functions.ps1' because it is being used by another process. 
  At line:46 char:14
  +     Copy-Item &lt;&lt;&lt;&lt;  -Path "$str_Share\Functions.ps1" -Destination $str_ScriptPath -Force
  </code></pre>
  
  <p>I don't see the reason why the error tells the local file is locked. It should be an unique file name and the file does not exist.<br>I think because the Copy-Item the source ($str_Share\Functions.ps1) is locked. <br>My question:<br>
  1) Is there a better way to handle this?<br>
  2) Or is there a workarround?<br>
  <br>
  Thanks for help<br>
  Patrick</p>