개요
New-Item(별칭 ni)은 새 항목을 만들고 그 값을 설정하는 cmdlet이다. 만들 수 있는 항목의 종류는 위치(프로바이더)에 따라 달라진다 — FileSystem 드라이브에서는 파일·디렉터리·심볼릭 링크를, Registry 드라이브에서는 레지스트리 키를, Alias: 드라이브에서는 별칭을 만든다.
사용법
| |
매개변수
| 매개변수 | 설명 |
|---|---|
-Path | 새 항목을 만들 위치. 이름까지 포함해 지정할 수도 있다 |
-Name | -Path와 별개로 이름을 지정(둘 다 지정 시 -Path 아래에 -Name으로 생성) |
-ItemType(별칭 -Type) | FileSystem: File, Directory, SymbolicLink, Junction, HardLink. Certificate: Certificate, Store, StoreLocation 등 프로바이더별로 다르다 |
-Value(별칭 -Target) | 새 항목의 초기 값(파일 내용, 또는 심볼릭 링크의 대상 경로) |
-Force | 읽기 전용 항목 덮어쓰기를 허용(단, 기존 디렉터리에 -Force를 다시 쓰면 내용은 그대로 두고 디렉터리 객체만 반환한다) |
예시
| |
주의사항·함정
파일에 대한 -Force와 디렉터리에 대한 -Force는 동작이 다르다: 이미 있는 파일 경로에 New-Item -Force를 다시 실행하면 기존 내용을 지우고 빈 파일로 덮어쓴다 — 레지스트리 키에 대해서도 마찬가지로 값이 전부 초기화된다. 반면 이미 있는 디렉터리 경로에 -Force를 실행하면 아무것도 지우지 않고 기존 디렉터리 객체를 그대로 반환한다. “존재하면 건드리지 않는다"는 가정으로 디렉터리에는 -Force를 안전하게 쓸 수 있지만, 같은 가정을 파일에 적용하면 데이터를 잃는다.
심볼릭 링크 생성은 권한이 필요할 수 있다: Windows에서 SymbolicLink 타입 생성은 기본적으로 관리자 권한이 필요하다. Windows 10(빌드 14972+)에서 개발자 모드를 켜면 일반 사용자 권한으로도 만들 수 있다.
-Path에는 와일드카드를 해석하지 않는다는 예외가 있다: 대부분의 프로바이더 cmdlet과 달리, New-Item의 -Path는 만들 대상의 경로 자체이므로 와일드카드 문자를 리터럴로 취급한다(예: 파일 이름에 *를 쓸 수 없다는 제약과 관련). 다만 여러 디렉터리에 동시에 만드는 예시처럼 상위 경로에 와일드카드를 써서 “이미 존재하는 여러 위치"를 가리키는 용도로는 여전히 와일드카드가 해석된다.
이식성: CMD의 md/type nul >, Bash의 mkdir/touch에 각각 대응하지만, PowerShell은 이 모든 것을 -ItemType 하나로 통일했고 레지스트리 키 생성까지 같은 cmdlet으로 처리한다는 점이 다르다.
![Featured image of post [PowerShell] 33. New-Item — 파일·디렉터리 생성](/post/powershell/new-item-command-create-file-directory-powershell/wordcloud_hu_c2c23155bf3172d0.webp)
![[PowerShell] 31. Get-ChildItem — 파일·디렉터리 목록](/post/powershell/get-childitem-command-file-directory-list-powershell/wordcloud_hu_3e00080e70b06aa7.webp)
![[PowerShell] 32. Set-Location/Push-Location/Pop-Location](/post/powershell/set-location-push-pop-location-powershell/wordcloud_hu_b555782207661429.webp)
![[PowerShell] 33. New-Item — 파일·디렉터리 생성](/post/powershell/new-item-command-create-file-directory-powershell/wordcloud_hu_c75a1ced5c6a3aa3.webp)
![[PowerShell] 34. Copy-Item — 복사](/post/powershell/copy-item-command-copy-file-powershell/wordcloud_hu_ebeda4bcc9a1c9ff.webp)
![[PowerShell] 35. Move-Item/Rename-Item — 이동과 이름 변경](/post/powershell/move-rename-item-command-powershell/wordcloud_hu_cb2a58d584784b24.webp)
![[PowerShell] 30. PSDrive와 프로바이더 개념](/post/powershell/psdrive-provider-concept-powershell/wordcloud_hu_48d84a64f7474c4.webp)
![[PowerShell] 36. Remove-Item — 삭제](/post/powershell/remove-item-command-delete-powershell/wordcloud_hu_85e27bcaf405bed4.webp)
![[PowerShell] 38. Get-ItemProperty/Set-ItemProperty](/post/powershell/get-set-itemproperty-command-powershell/wordcloud_hu_4fee0f6df596d89c.webp)