title: Set-Content : The process cannot access the file 'C:\WINDOWS\system32\drivers\etc\hosts' because it is being used by another process
body: <p>I have the following PowerShell script:</p>
  <pre><code>param([switch]$Elevated)
  
  function Test-Admin
  {
      $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
      $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
  }
  
  if ((Test-Admin) -eq $false)  {
      if ($elevated) {
          # tried to elevate, did not work, aborting
      } else {
          Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file &quot;{0}&quot; -elevated ' -f ($myinvocation.MyCommand.Definition))
      }
      exit
  }
  
  function UpdateHosts {
      param ($hostName)
  
      Write-Host $hostName
  
      try {
          $strHosts = (Get-Content C:\WINDOWS\system32\drivers\etc\hosts -Raw)
          if([string]::IsNullOrEmpty($strHosts)) {
              Write-Error &quot;Get-Content hosts empty&quot;
              exit
          }
      } catch {
          Write-Error &quot;Unable to read hosts file&quot;
          Write-Error $_
          exit
      }
  
      try {
          $strHosts -replace &quot;[\d]+\.[\d]+\.[\d]+\.[\d]+ $hostName&quot;,&quot;$ipAddress $hostName&quot; | Set-Content -Path C:\WINDOWS\system32\drivers\etc\hosts
      } catch {
          Write-Error &quot;Unable to write hosts file&quot;
          Write-Error $_ 
          exit
      }
  }
  
  $ipAddress = &quot;127.0.0.1&quot;
  
  UpdateHosts -hostName local.pap360.com
  </code></pre>
  <p>Sometimes, when I run it, I get the following error:</p>
  <blockquote>
  <p>Set-Content : The process cannot access the file 'C:\WINDOWS\system32\drivers\etc\hosts' because it is being used by another process.</p>
  </blockquote>
  <p>When I open up C:\WINDOWS\system32\drivers\etc\hosts in Notepad it's then blank. ie. all the data I had in it is wiped.</p>
  <p>My question is...  how can I prevent this from happening?</p>
  <p>Like if <code>Set-Content</code> can't access the hosts file to write to it then how is it able to wipe it's contents? And why isn't the <code>catch</code> block working?</p>
  <p>Here's the full error:</p>
  <pre><code>Set-Content : The process cannot access the file 'C:\WINDOWS\system32\drivers\etc\hosts' because it is being used by
  another process.
  At C:\path\to\test.ps1:36 char:92
  + ...  $hostName&quot; | Set-Content -Path C:\WINDOWS\system32\drivers\etc\hosts
  +                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : WriteError: (C:\WINDOWS\system32\drivers\etc\hosts:String) [Set-Content], IOException
      + FullyQualifiedErrorId : GetContentWriterIOError,Microsoft.PowerShell.Commands.SetContentCommand
  </code></pre>
  <p>I also don't understand why it's so intermittent. Is there some Windows process that opens the hosts file up for 1s once a minute or some such?</p>