programming ⌨/파워쉘 PowerShell

나를 위한 PowerShell(4) : 개인 설정과 ISE

Kortsec1 2024. 2. 28. 16:50

쉬어가는 느낌으로 PowerShell 개인 설정에 관해 다뤄볼 예정이다.

 

우리가 PowerShell을 실행하면 가장 먼저 만나는 화면은 Img1과 같을 것이다

Img1 _ Initial view

 

지금 내가 사용하는 환경이 보통의 경우와 조금 달라 정확하게 같진 않겠지만 크게 차이는 없다

여기서 개인 설정이라 함은 창 제목이라던지 실행 시 처음으로 출력되는 text와 같은 환경 값을 다룬다

이때 필요한 것이 PowerShell Profile이다.

 

 

PowerShell Profiles

Profile은 PowerShell이 시작될 때 실행되는 스크립트로, Users Documents 폴더의 PS1파일과 함께 실행된다

PS1파일에 관한 내용은 이전 포스팅을 참고하시길

2024.02.26 - [programming ⌨/파워쉘 PowerShell] - 나를 위한 PowerShell(3) : Execution Policy와 우회

 

개인 설정된 PS1 파일을 실행하기 위해 우선 ExecutePolicy부터 수정해 준다.

Set-ExecutionPolicy RemoteSigned

 

 

Configure Profile

New-Item명령으로 프로필을 생성해 볼 차례다.

New-Item -ItemType File -Path $Profile -Force

Img2 _ Create Profile

$PROFILEPowerShell 기본 프로필 위치를 담고있다.

확인해 보면 Img3과 같이 파일이 생성되었을 것이다

Img3 _ Created PowerShell Profile

 

파일 생성 시 모든 User에게 프로필을 적용하고 싶다면 코드를 아래와 같이 수정하면 된다

New-Item -ItemType File -Path $PROFILE.AllUsersAllHosts -Force

 

 

PowerShell ISE

프로필을 적용하기 위해 PowerShell Integrated Scripting Environment (ISE)라는 스크립트 에디터를 사용할 것이다

윈도에 기본적으로 깔려있으며, 콘솔에 ise를 입력하여 실행할 수 있다.

Img3 _ PowerShell ISE

 

ISE 에디터는 명령 작성, 수행은 물론 스크립트 디버깅과 같은 기능이 가능하다

또한 multiline editing, tab completion, syntax coloring과 같은 사용자 친화적인 그래픽을 지원한다.

Img4 _ Syntax Coloring

 

아까 생성한 프로필 파일을 건드려보자

ise $PROFILE

 

아래 예시 코드를 입력 후 실행

Write-Host 'Welcome to' "$env:computername" -ForegroundColor Green
Write-Host "You are logged in as" "$env:username"
Write-Host "Today:" (Get-Date)
Set-Location c:\
Write-Host "PowerShell"($PSVersionTable.PSVersion.Major)"awaiting your commands."

Img5 _ profile.ps1

 

$env환경변수를 의미한다

각자 원하는 스크립트를 구성한 후 저장해 보자.

 

이제 PowerShell을 새로 시작할 때마다 원하는 명령이 수행된다

Img6 _ Changed PowerShell Profile

 

 

요약


PowerShell Profiles : PowerShell이 시작될 때 실행되는 스크립트
" New-Item -ItemType File -Path $PROFILE -Force "
       { $PROFILE 위치에 파일 생성 }
$PROFILE : 프로필 파일의 위치 기본값
$PROFILE.AllUsersAllHosts : 모든 User에게 프로필 적용

PowerShell ISE : PowerShell 스크립트 에디터
        - 스크립트 디버깅 O
        - multiline editing
        - syntax coloring


 

 

https://sid-500.com/2018/01/16/powershell-for-beginners-part-6-powershell-profiles-and-the-ise/

https://learn.microsoft.com/ko-kr/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.4