title: Set-Content Error : The process cannot access the file because it is being used by another process (Powershell)
body: <p>What I'm trying to do here is delete all shared folders on my computer except for the default ones, which I'm removing with a single space before I loop through. It was working for about 10 minutes when all I of the sudden I get an error message saying that my Set-Content command won't go through because "it is being used by another process." Any ideas? Thanks in advance!</p>
  
  <p>Side Note: I know that <code>net share %%s /delete</code> is echoed out. I just wanted to see what will be deleted before I actually put it into effect.</p>
  
  <p><br/></p>
  
  <p><code>DeletingSharedFiles.bat</code>:</p>
  
  <pre><code>set /p name= "Name of the Admin user  |  "
  net share&gt; "C:\Users\%name%\Desktop\SharedFiles.txt"
  
  C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command 
  "C:\Users\%name%\Desktop\SharedFiles.ps1"
  
  for /f "tokens=1" %%s in (C:\Users\%name%\Desktop\SharedFiles.txt) do(
      echo net share %%s /delete  
  )
  </code></pre>
  
  <p><br/>
  <code>SharedFiles.ps1</code></p>
  
  <pre><code>(Get-Content "C:\Users\$env:name\Desktop\SharedFiles.txt") | 
  Foreach-Object {$_ -replace "ADMIN\$", ""} | 
  Foreach-Object {$_ -replace "C\$", ""} | 
  Foreach-Object {$_ -replace "IPC\$", ""} |  
  Set-Content "C:\Users\$env:name\Desktop\SharedFiles.txt" 
  </code></pre>
  
  <p><br/>
  Output of <code>DeletingSharedFiles.bat</code> </p>
  
  <pre><code>Set-Content : The process cannot access the file 
  'C:\Users\Sebastian\Desktop\SharedFiles.txt' because it is being used
  by another process.
  At C:\Users\Sebastian\Desktop\SharedFiles.ps1:5 char:1
  + Set-Content "C:\Users\$env:name\Desktop\SharedFiles.txt"
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo: NotSpecified: (:) [Set-Content], IOException
      + FullyQualifiedErrorId : 
  System.IO.IOException,Microsoft.PowerShell.Commands.SetContentCommand
  </code></pre>