Skip to content

Instantly share code, notes, and snippets.

@Nillth
Created March 2, 2023 15:11
Show Gist options
  • Save Nillth/7fd7850069894ef26db88421979b4098 to your computer and use it in GitHub Desktop.
Save Nillth/7fd7850069894ef26db88421979b4098 to your computer and use it in GitHub Desktop.
function Write-UTF8File
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.IO.FileInfo]$FilePath,
[Parameter(Mandatory = $true,
ValueFromPipeline = $true)]
[string[]]$String,
[Parameter(Mandatory = $false)]
[switch]$Force
)
begin
{
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
if ($FilePath.Exists -and $Force)
{
$FilePath.Delete()
}
}
process
{
if ($FilePath.Exists)
{
[System.IO.File]::AppendAllLines($FilePath.FullName, $String, $Utf8NoBomEncoding)
}
else
{
[System.IO.File]::WriteAllLines($FilePath.FullName, $String, $Utf8NoBomEncoding)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment