PowerShell 文件系统:完整生命周期与安全批处理
保留枚举、创建、复制移动删除、内容、属性和汇总,全部只在自建目录实跑。
学完这一章,你能够
- 保留枚举、创建、复制移动删除、内容、属性和汇总,全部只在自建目录实跑。
- 保留枚举、创建、复制移动删除、内容、属性和汇总,全部只在自建目录实跑。
列出和创建
Get-ChildItem可-Recurse、-Filter并区分-File/-Directory,-LiteralPath让[]等名称按字面处理;-Path可解释通配符。New-Item先创建必要父目录,-Force行为依Provider,不用它盲目覆盖。
复制、移动、删除和重命名
Copy-Item复制文件/目录时目的地存在与否影响嵌套形状;Move-Item和Rename-Item不自动解决冲突。Remove-Item -Recurse会删除目录内容,-Force不绕过权限。
本章每个目标在操作前解析为绝对路径并确认处于本工程新建目录内;演示-WhatIf不代表实际删除成功,真正删除也只针对本轮自建对象。
读写、存在性和属性
Get-Content默认行数组,-Raw读整个文本;Set-Content覆盖,Add-Content追加,Encoding按版本指定。Test-Path区分存在、Leaf和Container,Get-Item返回FileInfo/DirectoryInfo及Length/Extension/时间。
汇总与合并
固定.log文件用Measure-Object Length -Sum校验字节;文本合并先固定输入顺序且将输出置于输入目录之外,避免把新生成文件再次读进自身。零输入明确处理,不把空Sum误认为一定是数值0。
命令与参数参考
| 命令 | 关键输入 | 结果/作用 | 环境、错误与边界 |
| Get-ChildItem | -LiteralPath,-Recurse,-Filter,-File/-Directory | FileInfo/DirectoryInfo | 通配与字面路径区分 |
| New-Item | -Path,-ItemType File/Directory | 新资源 | 父目录/冲突及Provider条件 |
| Copy-Item | -LiteralPath,-Destination,-Recurse | 复制 | 目标是否已存在影响形状 |
| Move-Item/Rename-Item | -LiteralPath,-Destination/-NewName | 移动/重命名 | 预检绝对目标与冲突 |
| Remove-Item | -LiteralPath,-Recurse,-Force,-WhatIf | 删除或预演 | Force不绕过ACL,WhatIf不算删除 |
| Get-Content | -LiteralPath,-Raw可选 | 行序列/全文 | Encoding和换行 |
| Set-Content/Add-Content | -LiteralPath,-Value,-Encoding | 覆盖/追加 | 明确输出是否在输入集合外 |
| Test-Path | -LiteralPath,-PathType Leaf/Container | bool | 不存在/类型区分 |
| Get-Item | -LiteralPath | FileInfo及Length/Extension/时间 | 字节单位及文件/目录不同 |
| Measure-Object | Length -Sum | 总字节 | 空输入另处置 |
本章独立核心源码与验证步骤
展开查看收起
powershell
. (Join-Path $PSScriptRoot 'lab.ps1')
$root=New-LearningDirectory
try {
$source=New-Item -Path (Join-Path $root 'source') -ItemType Directory;$dest=New-Item -Path (Join-Path $root 'destination') -ItemType Directory
$file=Join-Path $source.FullName 'note [1].txt';Set-Content -LiteralPath $file -Value '一' -Encoding utf8;Add-Content -LiteralPath $file -Value '二' -Encoding utf8
$lines=@(Get-Content -LiteralPath $file);Assert-Learning ($lines.Count -eq 2 -and $lines[1] -eq '二') 'write append and literal brackets'
$copy=Assert-LearningPath -Path (Join-Path $dest.FullName 'copy.txt') -Root $root;Copy-Item -LiteralPath $file -Destination $copy
$moved=Assert-LearningPath -Path (Join-Path $dest.FullName 'moved.txt') -Root $root;Move-Item -LiteralPath $copy -Destination $moved;Assert-Learning (-not(Test-Path -LiteralPath $copy) -and (Test-Path -LiteralPath $moved)) 'move actual file'
Rename-Item -LiteralPath $moved -NewName 'renamed.txt';$renamed=Join-Path $dest.FullName 'renamed.txt';$info=Get-Item -LiteralPath $renamed;Assert-Learning ($info -is [IO.FileInfo] -and $info.Extension -eq '.txt') 'rename and FileInfo'
$copiedFolder=Assert-LearningPath -Path (Join-Path $root 'tree-copy') -Root $root;Copy-Item -LiteralPath $source.FullName -Destination $copiedFolder -Recurse
Assert-Learning (Test-Path -LiteralPath (Join-Path $copiedFolder 'note [1].txt') -PathType Leaf) 'recursive copy shape'
$log1=Join-Path $source.FullName 'a.log';$log2=Join-Path $source.FullName 'b.log';[IO.File]::WriteAllBytes($log1,[byte[]](1,2,3));[IO.File]::WriteAllBytes($log2,[byte[]](4,5))
$sum=Get-ChildItem -LiteralPath $source.FullName -Filter '*.log' -File | Measure-Object Length -Sum;Assert-Learning ($sum.Count -eq 2 -and $sum.Sum -eq 5) 'exact log bytes'
$output=Join-Path $root 'merged.txt';$inputs=@(Get-ChildItem -LiteralPath $source.FullName -Filter '*.txt' -File | Sort-Object Name);$inputs | ForEach-Object {Get-Content -LiteralPath $_.FullName} | Set-Content -LiteralPath $output -Encoding utf8
Assert-Learning (@(Get-Content -LiteralPath $output).Count -eq 2) 'merge outside input'
$safeDelete=Assert-LearningPath -Path $renamed -Root $root;Remove-Item -LiteralPath $safeDelete;Assert-Learning (-not(Test-Path -LiteralPath $renamed)) 'only owned file removed'
$escaped=$false;try{Assert-LearningPath -Path (Join-Path $root '../outside.txt') -Root $root}catch{$escaped=$true};Assert-Learning $escaped 'path escape rejected'
Write-LearningReport 'cmdlet-file-system-operations' @{lines=$lines;recursive_copy_contains_file=$true;log_count=$sum.Count;log_bytes=$sum.Sum;merge_rows=2;file_extension=$info.Extension;literal_brackets_handled=$true;path_escape_rejected=$escaped;owned_delete_observed=$true}
}finally{Remove-LearningDirectory $root}
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 字面[]文件写追加、复制/移动/改名、目录复制、日志精确5字节、合并2行及实际自有删除。
- 路径越界被拒绝,所有目标只在自建目录。
本章附件与实例包
课程关联工程包(登记关联16章) · 含当前章节或实例 · mbskycom202611a525.zip
常见错误与边界检查
- 把累计CPU秒数写成百分比。
- 把非终止错误、全局作用域或当前用户持久策略理解错。
- 用本地替代冒充真实服务/远程/账户/SQL/Office通过。
技术规范与官方资料
练习与参考解答
查看参考解答
先发现命令和参数,分清类型/输出流/作用域;副作用限定在自己创建的目标,未具备的管理环境不标完成。