개요
if/elseif/else는 조건에 따라 서로 다른 코드 블록을 실행하는 가장 기본적인 흐름 제어 구문이다. 20장의 비교 연산자(-eq, -gt 등)와 21장의 논리 연산자(-and, -or)가 만들어내는 참/거짓 값을 실제로 분기에 사용하는 자리가 바로 이 장이다.
정신 모델은 “각 조건을 위에서부터 순서대로 검사하다가, 처음으로 참(true)인 조건을 만나면 그 블록만 실행하고 나머지는 전부 건너뛴다"는 것이다. 어떤 조건도 참이 아니면 마지막 else 블록(있다면)이 실행된다.
사용법
| |
종류
| 형태 | 설명 |
|---|---|
단순 if | 조건이 참일 때만 실행, 거짓이면 아무 일도 안 함 |
if/else | 참·거짓 두 갈래 처리 |
if/elseif/…/else | 여러 조건을 순서대로 검사 |
삼항 연산자(? :) | PowerShell 7.0+, C# 스타일의 한 줄 조건식 |
예시
| |
주의사항·함정
삼항 연산자에서 명령을 실행하려면 반드시 괄호로 감싸야 한다: (조건) ? Write-Host 'a' : Write-Host 'b'처럼 참·거짓 분기에 명령을 그대로 쓰면 파싱 오류가 난다. (조건) ? (Write-Host 'a') : (Write-Host 'b')처럼 각 분기를 괄호로 감싸야 한다 — 삼항 연산자는 “값을 고르는 표현식"이지 “명령을 실행하는 문장"이 아니라는 설계 의도가 이 제약에 드러난다.
elseif가 길게 이어지면 가독성이 급격히 떨어진다: 같은 변수를 여러 값과 비교하는 elseif 체인이 4–5개를 넘어가면, 55장에서 다룰 switch 문으로 바꾸는 편이 훨씬 읽기 쉽고 관리하기도 편하다. 공식 문서도 elseif가 여러 개 필요한 상황에서는 switch를 권장한다.
조건식은 항상 불리언으로 강제 변환된다: if ($a)처럼 조건 자리에 불리언이 아닌 값(문자열, 숫자, 컬렉션)을 그대로 넣으면 PowerShell이 자동으로 참/거짓으로 변환한다 — 빈 문자열·0·$null·빈 배열은 거짓, 그 외 대부분은 참으로 취급된다. 이 자동 변환 규칙(41장에서 다룬 값-타입 원칙의 연장)을 모르면 if ($result)가 왜 예상과 다르게 동작하는지 헷갈리기 쉽다.
이식성: Bash의 if [ ... ]; then ... elif ... else ... fi, CMD의 if ... else if ... else와 개념은 같지만, PowerShell은 대괄호 대신 소괄호로 조건을 감싸고 then/fi 같은 종료 키워드 없이 중괄호로 블록을 구분한다는 문법 차이가 있다. 삼항 연산자는 Bash·CMD에 대응 문법이 없어 각각 [ 조건 ] && cmd1 || cmd2나 별도 if 블록으로 흉내 내야 한다.
![Featured image of post [PowerShell] 54. if/elseif/else — 조건 분기](/post/powershell/if-elseif-switch-condition-powershell/wordcloud_hu_49190db60079da02.webp)
![[PowerShell] 52. Get-Date와 TimeSpan](/post/powershell/get-date-timespan-powershell/wordcloud_hu_decf7202c9a21ab2.webp)
![[PowerShell] 53. 스크립트 작성과 실행(.ps1)](/post/powershell/script-execution-ps1-powershell/wordcloud_hu_10c8d6c3936ccc0f.webp)
![[PowerShell] 54. if/elseif/else — 조건 분기](/post/powershell/if-elseif-switch-condition-powershell/wordcloud_hu_fd68a53ddac2fa5a.webp)
![[PowerShell] 55. switch 고급 모드 — 와일드카드/정규식/파일 처리](/post/powershell/switch-wildcard-regex-file-powershell/wordcloud_hu_106a3f1b500a0d85.webp)
![[PowerShell] 56. foreach/for/while/do-while — 반복문](/post/powershell/foreach-for-while-loop-powershell/wordcloud_hu_658b84b145d1a138.webp)
![[PowerShell] 21. 논리 연산자와 조건식 조합 — -and/-or/-not](/post/powershell/logical-operators-and-or-not-powershell/wordcloud_hu_751168d59de9154c.webp)
![[PowerShell] 119. PowerShell 7의 크로스플랫폼 특징과 새 기능](/post/powershell/powershell-7-cross-platform-features/wordcloud_hu_f7049c437c0a91dc.webp)
![[PowerShell] 20. 비교 연산자 — -eq/-like/-contains/-in](/post/powershell/comparison-operators-eq-like-contains-in-powershell/wordcloud_hu_e39e3d06b7800d57.webp)
![[PowerShell] 39. Test-Path — 경로 존재 확인](/post/powershell/test-path-command-check-path-powershell/wordcloud_hu_8b7ea3ad5761442.webp)
![[PowerShell] 41. 변수와 데이터 타입](/post/powershell/variables-data-types-powershell/wordcloud_hu_6051f7a36d4700ee.webp)