Cmdlet File System Operations
File system operations are one of the most common and practical tasks in PowerShell.
From creating, copying, moving, and deleting files, to retrieving file attributes and batch processing directory structures, PowerShell provides numerous concise and powerful Cmdlets to accomplish these operations.
* * *
## 1. Viewing Directory Contents: `Get-ChildItem`
Get-ChildItem
This is the command in PowerShell for viewing directory contents (equivalent to `ls` or `dir`). By default, it lists all files and subdirectories in the current directory.
You can also use the path parameter:
Get-ChildItem -Path C:\Test
View all subdirectories (recursively):
Get-ChildItem -Path C:\Test -Recurse
Filter by file type:
Get-ChildItem -Path C:\Test -Filter *.txt
* * *
## 2. Creating Files and Directories: `New-Item`
Create a new directory:
New-Item -Path "C:\Demo" -ItemType Directory
Create a new file:
New-Item -Path "C:\Demo\example.txt" -ItemType File
> Tip: If the parent directory in the path does not exist, an error will occur. You need to manually create the parent directory first or use a script to do so.
* * *
## 3. Copying and Moving: `Copy-Item` and `Move-Item`
Copy a file:
Copy-Item -Path "C:\Demo\example.txt" -Destination "D:\Backup"
Copy an entire folder (including contents):
Copy-Item -Path "C:\Demo" -Destination "D:\Backup" -Recurse
Move a file:
Move-Item -Path "C:\Demo\example.txt" -Destination "C:\Demo2"
* * *
## 4. Deleting Files and Directories: `Remove-Item`
Delete a file:
Remove-Item -Path "C:\Demo\example.txt"
Delete a folder and its contents:
Remove-Item -Path "C:\Demo" -Recurse -Force
Explanation:
* `-Recurse`: Deletes all contents within the directory
* `-Force`: Forces deletion of hidden or read-only files
* * *
## 5. Reading and Writing Files: `Get-Content` / `Set-Content` / `Add-Content`
Read file contents:
Get-Content -Path "C:\Demo\example.txt"
Write content (overwrite):
Set-Content -Path "C:\Demo\example.txt" -Value "Hello PowerShell"
Append content:
Add-Content -Path "C:\Demo\example.txt" -Value "Another line"
* * *
## 6. File Renaming and Existence Check
Rename a file:
Rename-Item -Path "C:\Demo\example.txt" -NewName "renamed.txt"
Check if a file exists:
Test-Path -Path "C:\Demo\renamed.txt"
Returns True if it exists, otherwise returns False.
* * *
## 7. Getting File Attributes: `Get-Item`
$item = Get-Item -Path "C:\Demo\renamed.txt" $item.Length # File size (bytes) $item.CreationTime # Creation time $item.Extension # Extension
> Note: Returns a `System.IO.FileInfo` type object, which can be further used with object properties.
* * *
## 8. Combined Operation Example: Batch Processing Files
### Batch list total size of all `.log` files in a directory:
Get-ChildItem -Path "C:\Logs" -Filter *.log | Measure-Object -Property Length -Sum
Example output:
Count : 15Sum : 1289345Property : Length
Append contents of all .txt files to a new file:
Get-ChildItem -Path "C:\TextFiles" -Filter *.txt | ForEach-Object { Get-Content $_.FullName | Add-Content -Path "C:\AllText.txt"}
* * *
## 9. Notes and Common Pitfalls
| Issue | Explanation |
| --- | --- |
| Paths containing spaces | Use quotes `"` to enclose the entire path string |
| Distinguishing directories from files | `New-Item` must specify `-ItemType File` or `Directory` |
| Operation errors when file doesn't exist | Use `Test-Path` to check before processing |
| Be careful with delete commands | Use caution with `-Recurse` in `Remove-Item` to avoid accidental deletion |
* * *
## 10. Summary and Recommendations
PowerShell performs very powerfully in file system operations, with the following advantages:
* Intuitive and unified commands, low learning curve
* Supports object operations, can be combined with other commands
* Can be used for batch tasks and automation scripts
Recommended learning approach:
1. Practice various commands repeatedly in a temporary directory
2. Master five core commands: `New-Item`, `Remove-Item`, `Copy-Item`, `Get-Content`, `Set-Content`
3. Try encapsulating commands into scripts for batch processing of multiple files
YouTip