Skip to content
shellmap

new-itemPowerShell's create-file/directory cmdlet — like touch + mkdir across all 5 shells

Equivalents in every shell

Bashunix
touch file.txt

Touch creates an empty file (or updates the mtime of an existing one). For directories use `mkdir dir/`; `mkdir -p a/b/c` creates intermediate dirs. Bash has NO unified file/dir constructor — they're separate binaries with different flags and different idempotency rules.

Zshunix
touch file.txt

Same external binaries. macOS `touch` and `mkdir` are BSD-derived; `mkdir -p` and `touch` behave identically to GNU coreutils for everyday use. The `-r refFile` and `-d date` flags differ slightly between BSD touch and GNU touch.

Fishunix
touch file.txt

Same externals. Fish has no built-in file-creation primitive. Common idiom for ensuring a writable file path exists: `mkdir -p (dirname $path); and touch $path` — fish's `and` separator chains commands like `&&`.

PowerShellwindows
New-Item -ItemType File file.txt

PowerShell-native cmdlet (alias `ni`). `-ItemType` is the type discriminator: `File`, `Directory`, `SymbolicLink`, `HardLink`, `Junction`. `-Force` recreates intermediate parent directories (the `mkdir -p` analogue) AND overwrites an existing file — same flag, two different effects per `-ItemType`. PowerShell also exposes a `mkdir` FUNCTION that wraps `New-Item -ItemType Directory -Force`.

cmd.exewindows
type nul > file.txt

No single `touch`-equivalent — `type nul > file.txt` writes 0 bytes to create an empty file (overwrites if exists); `copy /b nul file.txt` is similar. For directories `md` / `mkdir` work; `mkdir a\b\c` creates intermediates automatically — no `-p` needed.

Worked examples

Create an empty file

Bash
touch newfile.txt
PowerShell
New-Item -ItemType File newfile.txt
cmd.exe
type nul > newfile.txt

Create a directory (and intermediates if missing)

Bash
mkdir -p a/b/c
PowerShell
New-Item -ItemType Directory -Force a/b/c
cmd.exe
mkdir a\b\c

Create a symlink pointing to another path

Bash
ln -s /target/path linkname
PowerShell
New-Item -ItemType SymbolicLink -Path linkname -Target /target/path
cmd.exe
mklink linkname \target\path

Gotchas

  • `New-Item -Force file.txt` OVERWRITES an existing file with an empty one — this is NOT what `touch -a file` does (touch just bumps the timestamp). To match touch's idempotent-create semantics: `if (-not (Test-Path file.txt)) { New-Item file.txt }`. For mtime-bumping use `(Get-Item file.txt).LastWriteTime = Get-Date`.
  • `-Force` is the `mkdir -p` analogue for directories (creates parents), but it's ALSO the overwrite flag for files — same flag, different effect per `-ItemType`. Read the `-ItemType` value before adding `-Force`; it's easy to nuke an existing file thinking you're being defensively idempotent.
  • Symlinks require ELEVATED Windows sessions OR Developer Mode enabled (`Settings → Privacy & security → For Developers → Developer Mode`). Without it, `New-Item -ItemType SymbolicLink` fails with `Administrator privilege required`. The same applies to cmd `mklink`. Linux / macOS `ln -s` has no such requirement.
  • The PowerShell `mkdir` function passes its argument to `New-Item -ItemType Directory -Force`, so `mkdir foo` silently SUCCEEDS if `foo` already exists — different from Unix `mkdir foo` which errors `File exists`. For strict-error behaviour use `New-Item -ItemType Directory foo` (no `-Force`).
  • `New-Item -Path file.txt -Value 'content'` initialises the file with content (create + write in one call). The default ENCODING is `Default` on PS 5.1 (system code page) and UTF-8 NO BOM on PS 7+ — different bytes on disk for non-ASCII. Specify `-Encoding UTF8` explicitly for cross-version reproducibility.

WSL & PowerShell Core notes

pwsh`New-Item` works the same on every pwsh platform — but `-ItemType SymbolicLink` on Windows pwsh still needs admin / Developer Mode, while on Linux / macOS pwsh it works as any user. The `mkdir` function alias exists on every pwsh; the `md` alias is Windows-only (a cmd-era legacy carried into pwsh on Windows).
WSLSymlinks created inside WSL `~/` are native Unix symlinks. Symlinks created on `/mnt/c/...` may or may not work depending on whether Windows Developer Mode is enabled — `ln -s target /mnt/c/Users/.../link` errors `Operation not permitted` otherwise. For symlink-heavy workflows, build the tree inside the Linux filesystem and copy at deploy time.

Related commands