PoshCode Archive  Artifact Content

Artifact 809b25be76d09ed13e35d0862a137ea5673cdab3cefa9d371b0eecd47bd73525:

  • File Set-Encoding.ps1 — part of check-in [09359ed9d1] at 2018-06-10 13:08:13 on branch trunk — Provides an easy way to change the text encoding on a lot of scripts at once. (user: Jason Ferris size: 2553)

# encoding: ascii
# api: powershell
# title: Set-Encoding
# description: Provides an easy way to change the text encoding on a lot of scripts at once.
# version: 0.1
# type: function
# author: Jason Ferris
# license: CC0
# function: Set-Encoding
# x-poshcode-id: 2288
# x-archived: 2017-05-16T03:34:57
# x-published: 2011-10-07T07:22:00
#
# It calls the Out-File command with the -Encoding parameter and the -Force switch. 
# Very useful for fixing problems when trying to sign non-ASCII format files.
# Note:  Don’t use on your MP3’s or other non-text files :)
#
function Set-Encoding{
<#
.Synopsis
   Takes a Script file or any other text file into memory and Re-Encodes it in the format specified.
.Parameter Source
   The path to the file to be re-encoded.
.Parameter Destination
   The path to write the corrected file to
.Parameter Encoding 
   The encoding to convert to
   Note:
   Default  Uses the encoding of the system's current ANSI code page.
   OEM      Uses the current original equipment manufacturer code page identifier for the operating system.
.Example
  ls *.ps1 | Set-Encoding ASCII
.Example
  ls *.ps1 | Set-Encoding UTF8 -Destination { [System.IO.Path]::ChangeExtension($_.FullName, ".utf8.ps1") }

.Description
   Provides an easy way to change the text encoding on a lot of scripts at once.
.Notes
   Don't use on non-text files!
   The script just calls Out-File with the -Encoding parameter and the -Force switch, but it's very useful for making sure all your script files are correctly UTF8 or ASCII encoded so they can be signed.
#>

[CmdletBinding()]

PARAM(
   [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false)]
   [ValidateSet("Unicode","UTF7","UTF8","UTF32","ASCII", "BigEndianUnicode","Default", "OEM")]
   [string]$Encoding
,
   [Parameter(Position=1, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
   [ValidateScript({ 
      if((resolve-path $_).Provider.Name -ne "FileSystem") {
         throw "Specified Path is not in the FileSystem: '$_'" 
      }
      return $true
   })]
   [Alias("Fullname","Path","File")]
   [string]$Source
,
   [Parameter(Position=2, Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
   [string]$Destination = $Source
,
   [Switch]$Passthru
)
PROCESS{
   Write-Verbose "Encoding content from '$Source' into '$Destination' with $Encoding encoding."
   (Get-Content $Source) | Out-File -FilePath $Destination -Encoding $Encoding -Force
   if($Passthru){ Get-Item $Destination }
}
}