蓝色天空网mbsky.com · 知识 · 实践 · 创造PowerShell · 本地阅读预览
教程 / .NET / PowerShell

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/-DirectoryFileInfo/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/Containerbool不存在/类型区分
Get-Item-LiteralPathFileInfo及Length/Extension/时间字节单位及文件/目录不同
Measure-ObjectLength -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}
PowerShell 文件系统:完整生命周期与安全批处理:本章独立本地实验

所需环境:Windows / PowerShell7.6.5 / 同包自有文件、进程、回环JSON;其它平台和管理环境待验

当前源码 SHA256:36ebedd1183bb30393023d782d4132a94ca99b492e81dfeae6fefa77fdd8486d

下载本站示例源码
预期结果 · 待实际运行核验
  • 字面[]文件写追加、复制/移动/改名、目录复制、日志精确5字节、合并2行及实际自有删除。
  • 路径越界被拒绝,所有目标只在自建目录。

常见错误与边界

  • 把累计CPU秒数写成百分比。
  • 把非终止错误、全局作用域或当前用户持久策略理解错。
  • 用本地替代冒充真实服务/远程/账户/SQL/Office通过。

练习与解答

仅在新建测试目录改变一个固定输入,先列出预期、目标及清理方式,再核对真实输出。

展开参考解答

先发现命令和参数,分清类型/输出流/作用域;副作用限定在自己创建的目标,未具备的管理环境不标完成。

规范与官方依据