Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Cmdlet File System Operations

Cmdlet File System Operations

File system operations are among the most common and practical tasks in PowerShell.

From creating, copying, moving, and deleting files to getting file properties and batch-processing directory structures, PowerShell provides many concise and powerful Cmdlets to accomplish these operations.


I. Viewing directory contents:Get-ChildItem

Get-ChildItem

This is the command in PowerShell to view directory contents (equivalent to ls or dir). It lists all files and subdirectories in the current directory by default.

You can also use a 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


II. 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 be reported. You must create the upper-level directory manually or via a script first.


III. 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"


IV. 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:


V. Reading and writing files:Get-Content / Set-Content / Add-Content

Read file content:



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"


VI. 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 False.


VII. Getting file properties:Get-Item

$item = Get-Item -Path "C:\Demo\renamed.txt"

$item.Length         # 文件大小(字节)

$item.CreationTime   # 创建时间

$item.Extension      # 扩展名

Note: what is returned is a System.IO.FileInfo type object, whose object properties can be used further.


VIII. Combined operation example: batch processing files

Batch list all .log files' total size:

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

Output result example:

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"

}


IX. Notes and common pitfalls

Problem Description
Path contains spaces Use quotes " to wrap the entire path string
Distinguish directories and files New-Item You must specify -ItemType File or Directory
Error when file does not exist Use Test-Path Check before processing
Deletion command requires caution Remove-Item Use -Recurse Be cautious when using

X. Summary and suggestions

PowerShell is very powerful in file system operations, with the following advantages:

Suggested learning approach:

  1. Practice various commands repeatedly in a temporary directory
  2. Master the five core commands:New-Item, Remove-Item, Copy-Item, Get-Content, Set-Content
  3. Try encapsulating commands into scripts to batch process multiple files