본문 바로가기

파워쉘

[파워쉘] UTF-8 without BOM 인코딩으로 파일 저장


파워쉘, 아니 윈도우즈는 기본적으로 UTF-8 형태로 저장할 때 UTF-8 with BOM 으로 저장한다.

그래서 그 파일이 다른 기종에서 읽혀질 때 문제가 발생할 수 있다.


필자는 iOS의 info.plist 파일때문에 이 개같은 경우를 당했다.


서핑을 해보니 아래와 같이 처리하면 된다고 함.

### ps

$infoPlistFile = "C:\temp\abc.txt"

$infoPlistContent = "파일의 내용이다."


# 파워쉘은 UTF-8 with BOM 형식으로 자동으로 저장한다.

Set-Content -Path $infoPlistFile -Value $infoPlistContent -Encoding "UTF8"

# 그래서 아래와 같이 다시 without BOM 처리를 해주어야 한다.

$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)

[System.IO.File]::WriteAllLines($infoPlistFile, $infoPlistContent, $Utf8NoBomEncoding)