Manipulate Item
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 DirectoryPowershell 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 SymbolicLinkNOTE
-Target is an alias of -Value
TIP
Use -Force flag to overwrite existing target.
Delete
- delete file
ri <file>
gci -file | ri2
- delete folder
ri -rec -force <folder>
gci -dir | ri -rec -force2
Rename
rni <item> -NewName { $_.Name -replace 'foo', 'bar' }
gci -file | rni -NewName { $_.Name -replace 'foo', 'bar' }2