📁 PowerShell 文件和文件夹操作
一、基础概念
在 PowerShell 中,文件和文件夹被当作对象进行操作,利用一系列 cmdlet(命令行小程序)可以高效地完成文件系统管理任务,如创建、复制、移动、删除、重命名等。
二、文件和文件夹常用操作命令
1. Get-ChildItem(列出目录内容)
作用:列出指定路径中的文件和文件夹(类似于 dir 或 ls)。
命令格式:
Get-ChildItem [-Path] <string> [-Recurse] [-File] [-Directory]
常用参数:
| 参数 | 说明 |
|---|---|
-Path |
指定路径 |
-Recurse |
递归列出所有子目录中的项目 |
-File |
仅返回文件 |
-Directory |
仅返回文件夹 |
示例:
Get-ChildItem "C:\Users" # 列出C:\Users下所有内容
Get-ChildItem -Path "C:\Test" -File # 仅列出文件
Get-ChildItem -Recurse # 递归列出当前目录及子目录
2. New-Item(创建文件或文件夹)
作用:创建新的文件或文件夹。
命令格式:
New-Item -Path <string> -ItemType <File|Directory> [-Name <string>] [-Value <string>]
常用参数:
| 参数 | 说明 |
|---|---|
-Path |
目标路径 |
-ItemType |
项目类型:File 或 Directory |
-Name |
文件或文件夹名称 |
-Value |
初始内容(仅对文件适用) |
示例:
New-Item -Path "C:\Test\example.txt" -ItemType File
New-Item -Path "C:\Test\NewFolder" -ItemType Directory
3. Remove-Item(删除文件或文件夹)
作用:删除文件、文件夹或其他项目。
命令格式:
Remove-Item [-Path] <string> [-Recurse] [-Force]
常用参数:
| 参数 | 说明 |
|---|---|
-Recurse |
递归删除目录及其内容(必须小心使用) |
-Force |
删除隐藏或只读文件 |
示例:
Remove-Item "C:\Test\example.txt"
Remove-Item "C:\Test\NewFolder" -Recurse -Force
4. Copy-Item(复制文件或文件夹)
作用:复制文件或文件夹到目标位置。
命令格式:
Copy-Item -Path <string> -Destination <string> [-Recurse] [-Force]
常用参数:
| 参数 | 说明 |
|---|---|
-Destination |
目标路径 |
-Recurse |
递归复制整个目录结构 |
-Force |
覆盖只读文件或现有文件 |
示例:
Copy-Item "C:\Test\example.txt" -Destination "D:\Backup"
Copy-Item "C:\Test\Folder" -Destination "D:\Backup" -Recurse
5. Move-Item(移动/重命名文件或文件夹)
作用:将文件或文件夹移动到新位置,或重命名。
命令格式:
Move-Item -Path <string> -Destination <string> [-Force]
常用参数:
| 参数 | 说明 |
|---|---|
-Destination |
新位置或新名称 |
-Force |
强制覆盖目标位置已有文件 |
示例:
Move-Item "C:\Test\example.txt" -Destination "D:\Data\example.txt"
Move-Item "C:\Test\file1.txt" -Destination "C:\Test\file2.txt" # 重命名
6. Rename-Item(重命名文件或文件夹)
作用:重命名文件或文件夹。
命令格式:
Rename-Item -Path <string> -NewName <string>
示例:
Rename-Item -Path "C:\Test\oldname.txt" -NewName "newname.txt"
7. Set-Content 与 Get-Content(写入/读取文件内容)
Set-Content(写入或替换文件内容)
格式:
Set-Content -Path <string> -Value <string>
示例:
Set-Content -Path "C:\Test\example.txt" -Value "Hello, PowerShell!"
Get-Content(读取文件内容)
格式:
Get-Content -Path <string>
示例:
Get-Content -Path "C:\Test\example.txt"
8. Out-File(将输出写入文件)
作用:将命令输出重定向到文件。
格式:
<command> | Out-File -FilePath <string>
示例:
Get-Process | Out-File -FilePath "C:\Test\processes.txt"
9. Test-Path(检索文件夹或文件)
命令格式:
Test-Path -Path <路径>
说明:
-
Test-Path返回布尔值:存在返回True,不存在返回False。 -
适用于文件、文件夹、注册表路径等。
参数:
| 参数 | 说明 |
|---|---|
-Path |
要检查的路径 |
-PathType |
限定要检查的是文件还是目录 |
-PathType 的取值:
-
Any:默认,文件或文件夹都可以 -
Leaf:表示文件 -
Container:表示文件夹(目录)
示例 1:检查文件是否存在
Test-Path -Path "C:\Test\file.txt"
# 输出:True 或 False
示例 2:检查文件夹是否存在
Test-Path -Path "C:\Test\Logs"
示例 3:明确判断是否为文件或文件夹
Test-Path -Path "C:\Test\file.txt" -PathType Leaf # 判断是否为文件
Test-Path -Path "C:\Test\Logs" -PathType Container # 判断是否为文件夹
示例 4:用于 if 条件语句
if (Test-Path "C:\MyFolder") {
Write-Output "文件夹存在"
} else {
Write-Output "文件夹不存在"
}
三、实用技巧
-
通配符使用:
*表示任意字符,?表示一个字符。Get-ChildItem -Path "C:\Test\*.txt" # 所有 txt 文件 Remove-Item "C:\Test\file?.txt" # 删除 file1.txt, fileA.txt 等 -
组合命令示例:
Get-ChildItem -Recurse -File | Where-Object { $_.Length -gt 1MB } -
管道与过滤:
Get-ChildItem | Where-Object { $_.Extension -eq ".log" } | Remove-Item
四、总结表格
| 操作 | 命令 | 说明 |
|---|---|---|
| 列出内容 | Get-ChildItem |
类似 dir/ls |
| 创建 | New-Item |
创建文件或文件夹 |
| 删除 | Remove-Item |
删除文件或目录 |
| 复制 | Copy-Item |
复制文件或目录 |
| 移动/重命名 | Move-Item |
移动或重命名 |
| 重命名 | Rename-Item |
更改文件/目录名称 |
| 读文件 | Get-Content |
读取文本文件 |
| 写文件 | Set-Content |
写入文本(覆盖) |
| 输出文件 | Out-File |
将命令输出保存为文件 |
| 判断文件 | Test-Path |
判断文件或文件夹是否存在 |

浙公网安备 33010602011771号