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 obtaining file attributes and batch processing directory structures, PowerShell provides a large number of concise and powerful Cmdlets to accomplish these operations.


1. View Directory Contents:Get-ChildItem

Get-ChildItem

This is the command in PowerShell to view directory contents (equivalent tolsordir) command. 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 (recursive):

Get-ChildItem -Path C:\Test -Recurse

Filter by file type:

Get-ChildItem -Path C:\Test -Filter *.txt

2. Create 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 be reported. You need to manually or via script create the parent directory first.


3. Copy and Move:Copy-ItemandMove-Item

Copy a file:

Copy-Item -Path "C:\Demo\example.txt" -Destination "D:\Backup"

Copy an entire folder (including its contents):

Copy-Item -Path "C:\Demo" -Destination "D:\Backup" -Recurse

Move a file:

Move-Item -Path "C:\Demo\example.txt" -Destination "C:\Demo2"

4. Delete 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: Delete all contents under the directory
  • -Force: Force delete hidden or read-only files

5. Read and Write 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 Checking

Rename a file:

Rename-Item -Path "C:\Demo\example.txt" -NewName "renamed.txt"

Determine whether a file exists:

Test-Path -Path "C:\Demo\renamed.txt"

Returns True if it exists, otherwise returns False.


7. Get File Properties:Get-Item

$item = Get-Item -Path "C:\Demo\renamed.txt"
$item.Length         # 文件大小(字节)
$item.CreationTime   # 创建时间
$item.Extension      # 扩展名

Note: The return is aSystem.IO.FileInfotype object, and you can further use the object's properties.


8. Combined Operation Example: Batch Processing Files

Batch list all in the directory.logTotal file size:

Get-ChildItem -Path "C:\Logs" -Filter *.log | Measure-Object -Property Length -Sum

Example output:

Count    : 15
Sum      : 1289345
Property : Length

Append all .txt file contents to a new file:

Get-ChildItem -Path "C:\TextFiles" -Filter *.txt | ForEach-Object {
    Get-Content $_.FullName | Add-Content -Path "C:\AllText.txt"
}

9. Precautions and Common Pitfalls

Issue Description
Spaces in path Use quotes"to enclose the entire path string
Distinguish directories and files New-ItemMust specify-ItemType FileorDirectory
Operation errors when file does not exist UseTest-Pathto check before processing
Be careful with delete commands Remove-ItemUse-Recursewith caution to avoid accidental deletion

10. Summary and Suggestions

PowerShell is very powerful in file system operations and has the following advantages:

  • Commands are intuitive and unified, with a low learning cost
  • Supports object operations and can be combined with other commands
  • Can be used for batch tasks and automation scripts

Suggested learning approach:

  1. Repeatedly practice various commands in a temporary directory
  2. Master the five core commands:New-Item、Remove-Item、Copy-Item、Get-Content、Set-Content
  3. Try to encapsulate commands into scripts to batch process multiple files
Other extensions