Create Item
Powershell uses a single cmdlet named New-Item
to represent all kinds of creation logic for file system items like folder, file, symlinks...
TIP
Use ni
alias for New-Item
.
File
New-Item
creates file without extra options by default.
New-Item -Path <file_path>
# or
New-Item <file_path>
2
3
NOTE
-Path
is the first positional parameter which can be ignored.
NOTE
-Path
is string[]
, so you can create multiple items at a same time with same options.
TIP
Use -Force
flag to overwrite existing target.
Directory
New-Item <dir_path> -ItemType Directory
Powershell has another builtin function called mkdir
, it's a shorthand for New-Item <dir_path> -ItemType Directory
mkdir <dir_path>
NOTE
When creating directory, New-Item
ensures necessary parent directories by default, so there's no things like mkdir -p
.
TIP
Use -Force
flag to overwrite existing target.
Symbolic Link
New-Item <symlink_path> -Target <source> -ItemType SymbolicLink
TIP
Use -Force
flag to overwrite existing target.
Ignore Wildcards
-Path
translates wildcards by default, if you do need to include special characters from wildcards syntax for your new item, use -LiteralPath
.
New-Item -LiteralPath 'foo*.txt' # creates a file literally named `foo*.txt`