본문 바로가기

Windows/PowerShell

[PowerShell] 파워쉘에서 한글 깨짐 문제 해결

반응형

파워쉘이나 Windows Terminal의 파워쉘 탭에서 한글이 깨지는 경우가 있습니다.

Git 로그를 출력하는 경우 한글이 깨지거나 아래와 같이 한글 입력이 정상적으로 표시되지 않는 경우입니다.

한글 깨짐

한글이 깨지는 문제를 해결하려면 아래 명령어를 파워쉘에서 입력하면 됩니다.

$env:LC_ALL='C.UTF-8'
[System.Console]::InputEncoding = [System.Console]::OutputEncoding = [System.Text.Encoding]::UTF8

다만 이 방법은 새로운 파워쉘을 여는 경우 초기화되기 때문에 매번 다시 입력해줘야 합니다.

이런 경우 파워쉘 프로필을 활용해서 설정을 영구적으로 반영할 수 있습니다.

파워쉘 프로필(Profile)은 새로운 파워쉘 세션이 생길 때마다 실행되는 명령어들을 정의할 수 있습니다.

아래 명령어로 적용 가능한 프로필들을 확인할 수 있습니다.

$PROFILE | Get-Member -Type NoteProperty

아래와 같이 결과가 표시되는 것을 확인할 수 있습니다.

프로필 경로

현재 사용자와 현재 호스트는 $PROFILE에 저장되어 있으며 각각의 사용자와 호스트에 대한 설정은 아래와 같습니다.

Current User, Current Host

$PROFILE

$PROFILE.CurrentUserCurrentHost

Current User, All Hosts

$PROFILE.CurrentUserAllHosts

All Users, Current Host

$PROFILE.AllUsersCurrentHost

All Users, All Hosts

$PROFILE.AllUsersAllHosts

모든 유저와 모든 호스트에 반영하려면 $PROFILE.AllUsersAllHosts에 적용하면 됩니다.

PowerShell을 반드시 관리자 권한으로 켜고 아래 스크립트를 실행합니다.

if (!(Test-Path -Path $PROFILE.AllUsersAllHosts)) {
  New-Item -ItemType File -Path $PROFILE.AllUsersAllHosts -Force
  "`$env:LC_ALL='C.UTF-8'" | 
    Out-File -FilePath $PROFILE.AllUsersAllHosts -Append
  "[System.Console]::InputEncoding = [System.Console]::OutputEncoding = [System.Text.Encoding]::UTF8" | 
    Out-File -FilePath $PROFILE.AllUsersAllHosts -Append
}

위의 설정 반영 후 파워쉘에는 반영되지만 Windows Terminal 실행 시 아래와 같은 에러를 보게 됩니다.

Windows Terminal 에러

. : File C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1 cannot be loaded. The file
C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1 is not digitally signed. You cannot run this script on the
current system. For more information about running scripts and setting execution policy, see about_Execution_Policies
at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:3
+ . 'C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1'
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

이런 경우 아래 명령어로 현재 허용되는 스크립트를 확인합니다.

Get-ExecutionPolicy -list

아래와 같이 AllSigned가 표시되면 모든 스크립트가 서명된 상태인 경우에만 실행됩니다.

실행 정책 확인

관리자 권한으로 파워쉘을 켠 상태에서 아래 명령어로 정책을 변경해 주면 정상적으로 실행이 됩니다.

Set-ExecutionPolicy RemoteSigned

RemoteSigned는 로컬에 저장된 스크립트는 서명없이도 실행할 수 있도록 해줍니다.

이것으로 파워쉘과 Windows Terminal의 한글 깨짐 현상이 해결됩니다.

반응형