title: PowerShell - Batch change files encoding To UTF-8
body: <p>I'm trying to do a dead simple thing: to change files encoding from anything to UTF-8 without BOM. I found several scripts that do this and the only one that really worked for me is this one: <a href="https://superuser.com/questions/397890/convert-text-files-recursively-to-utf-8-in-powershell#answer-397915">https://superuser.com/questions/397890/convert-text-files-recursively-to-utf-8-in-powershell#answer-397915</a>.</p>
  
  <p>It worked as expected, but I need the generated files without BOM. So I tried to modify the script a little, adding the solution given to this question: <a href="https://stackoverflow.com/questions/5596982/using-powershell-to-write-a-file-in-utf-8-without-the-bom/#answer-5596984">Using PowerShell to write a file in UTF-8 without the BOM</a> </p>
  
  <p>This is my final script:</p>
  
  <pre><code>foreach ($i in Get-ChildItem -Recurse) {
      if ($i.PSIsContainer) {
          continue
      }
  
      $dest = $i.Fullname.Replace($PWD, "some_folder")
  
      $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
  
      if (!(Test-Path $(Split-Path $dest -Parent))) {
          New-Item $(Split-Path $dest -Parent) -type Directory
      }
  
      get-content $i | out-file -encoding $Utf8NoBomEncoding -filepath $dest
  }
  </code></pre>
  
  <p>The problem is that powershell is returning me an error, regarding the <code>System.Text.UTF8Encoding($False)</code> line, complaining about an incorrect parameter:</p>
  
  <p><em>It is not possible to validate the argument on the 'Encoding' parameter. The argument "System.Text.UTF8Encoding" dont belongs to the the group "unicode, utf7, utf8, utf32, ascii" specified by the ValidateSet attribute.</em></p>
  
  <p>I wonder if I'm missing something, like powershell version or something like that. I never coded a Powershell script before, so I'm totally lost with this. And I need to change these files encoding, there are hundreds of them, I wouldn't like to do it myself one by one.</p>
  
  <p>Actually I'm using the 2.0 version that comes with Windows 7.</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>I tried the following code, suggested by @LarsTruijens and other posts:</p>
  
  <pre><code>$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
  foreach ($i in Get-ChildItem -Recurse) {
      if ($i.PSIsContainer) {
          continue
      }
  
      $dest = $i.Fullname.Replace($PWD, "some_folder")
  
      if (!(Test-Path $(Split-Path $dest -Parent))) {
          New-Item $(Split-Path $dest -Parent) -type Directory
      }
  
      $content = get-content $i
      [System.IO.File]::WriteAllLines($dest, $content, $Utf8NoBomEncoding)
  }
  </code></pre>
  
  <p>This gives me an Exception, complaining about one of the parameters for WriteAllLines: <code>"Exception on calling 'WriteAllLines' with 3 arguments. The value can't be null". Parameter name: contents</code>. The script creates all folders, though. But they are all empty.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>An interesting thing about this error is that the "content" parameter is not null. If I output the value of the $content variable (using Write-host) the lines are there. So why it becomes null when passed to WriteAllLines method? </p>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>I've added a content check to the variable, so the script now looks like this:</p>
  
  <pre><code>$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
  foreach ($i in Get-ChildItem -Recurse) {
      if ($i.PSIsContainer) {
          continue
      }
  
      $dest = $i.Fullname.Replace($PWD, "some_folder")
  
      if (!(Test-Path $(Split-Path $dest -Parent))) {
          New-Item $(Split-Path $dest -Parent) -type Directory
      }
  
      $content = get-content $i
  
      if ( $content -ne $null ) {
  
          [System.IO.File]::WriteAllLines($dest, $content, $Utf8NoBomEncoding)
      }
      else {
          Write-Host "No content from: $i"
      }
  }
  </code></pre>
  
  <p>Now every iteration returns "No content from: $i" message, but the file isn't empty. There is one more error: <code>Get-content: can't find the path 'C:\root\FILENAME.php' because it doesn't exists.</code> It seems that it is trying to find the files at the root directory and not in the subfolders. It appears to be able to get the filename from child folders, but tries to read it from root.</p>
  
  <p><strong>EDIT 4 - Final Working Version</strong></p>
  
  <p>After some struggling and following the advices I got here, specially from @LarsTruijens and  @AnsgarWiechers, I  finally made it. I had to change the way I was getting the directory from $PWD and set some fixed names for the folders. After that, it worked perfectly.</p>
  
  <p>Here it goes, for anyone who might be interested:</p>
  
  <pre><code>$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
  $source = "path"
  $destination = "some_folder"
  
  foreach ($i in Get-ChildItem -Recurse -Force) {
      if ($i.PSIsContainer) {
          continue
      }
  
      $path = $i.DirectoryName -replace $source, $destination
      $name = $i.Fullname -replace $source, $destination
  
      if ( !(Test-Path $path) ) {
          New-Item -Path $path -ItemType directory
      }
  
      $content = get-content $i.Fullname
  
      if ( $content -ne $null ) {
  
          [System.IO.File]::WriteAllLines($name, $content, $Utf8NoBomEncoding)
      } else {
          Write-Host "No content from: $i"   
      }
  }
  </code></pre>