PowerShell教程 - 文件系统管理(File System Management)
更新记录
转载请注明出处。
2022年8月24日 发布。
2022年8月18日 从笔记迁移到博客。
文件系统管理(File System Management)
文件类型说明#
与Linux不同,Windows下只有3种文件类型:
磁盘驱动器(Diver)
文件夹(Folder)
文件(File)
磁盘驱动器是最上层的对象,包含文件夹和文件
文件夹是一种容器对象,它可以包含文件以及其他文件夹
文件不是一种容器对象,该对象处于层级的末尾,用于表示具体的数据
注意:
因为Powershell使用Provider来操作各种数据
所以PowerShell中的术语和文件系统中的略有不同
PSDrive可能不是指向某个文件系统,比如PSDrive可以映射到注册表
所以PowerShell并不会使用“文件”以及“文件夹”的说法
PowerShell采用更通俗的说法:“项”(Item)
一个文件或者一个文件夹都叫作项(Item)
每个项基本上都会存在对应的属性
比如,一个文件项可能有最后写入的时间、是否只读等属性
一些项,比如文件夹,可能包含子项(子项包含在文件夹项中)
Item名词对应的是单独对象,比如文件或者文件夹
ItemProperty代表一个项对应的属性。比如只读、项创建时间、长度等
ChildItem名词对应一个项(比如文件)包含于另外一个项(文件夹)中
FileSystem不支持事务,所以文件系统下的Cmdlet不支持-UseTransaction参数
获得项#
Get-Item
实例:
Get-Item \ # The root container
Get-Item . # The current container
Get-Item .. # The parent container
Get-Item C:\Windows\System32\cmd.exe # A leaf item
Get-Item Cert:\LocalMachine\Root #A container item
Get-Item $env:USERPROFILE\AppData -Force
获得剪切板的内容#
Get-Clipboard
设置剪切板的内容#
Set-Clipboard
实例:
Set-Clipboard "Panda666.com"
设置当前路径#
Set-Location
实例:
Set-Location \ # The root of the current drive
Set-Location Windows # A child container named Windows
Set-Location .. # Navigate up one level
Set-Location ..\.. # Navigate up two levels
Set-Location Cert: # Change to a different drive
Set-Location HKLM:\Software # Change to a specific child
获得当前路径#
Get-Location
还可以使用$pwd预定义常量
$pwd
注意:.NET类型不受路径影响,需要加上$pwd常量
[System.IO.File]: :WriteAllLines("$pwd\file.txt", "Some content'"
获得子目录子文件#
Get-ChildItem
Get-ChildItem -Path D:/ #指定路径,默认为当前路径
只查看文件
Get-ChildItem -File
只查看目录
Get-ChildItem -Directory
只查看隐藏文件
Get-ChildItem -Hidden
只查看只读文件
Get-ChildItem -ReadOnly
查看深度
Get-ChildItem -Depth 2
遍历查看
Get-ChildItem -Recurse
过滤文件
Get-ChildItem -Filter "Panda*"
注意:部分CmdLet不支持-Filter,可以使用以下方式
即:将 -Exclude -Include和-Recurse 配合使用
命令 D:/ -Exclude "Panda*" -Recurse -Depth 1
验证路径存在#
Test-Path
实例:
Test-Path D:\temp\test
Test-Path HKLM:\Software\Publisher
Test-Path C:\Windows -PathType Container
Test-Path C:\Windows\System32\cmd.exe -PathType Leaf
测试文件夹存在
if (-not (Test-Path C:\Temp\NewDirectory -PathType Container)) {
New-Item C:\Temp\NewDirectory -ItemType Directory
}
注意:Some files in Windows are locked, with the result that Get-Item and Test-Path are unable to correctly return results
可以使用.NET的File静态类的Exists方法替代
[System.IO.File]::Exists('c:\pagefile.sys')
创建文件#
New-Item
实例:
创建文本文件
New-Item -ItemType File "D:/test2.txt"
创建文本文件并写入内容
New-Item -ItemType File "D:/test2.txt" -Value "Panda666.com"
创建文件夹
New-Item -ItemType Directory "D:/test"
New-Item -ItemType Directory -Path "D:/" -Name "Panda"
强制创建文件(覆盖同名文件)
New-Item -Path 'testForTest.txt' -Force
创建文件并指定是创建文件
New-Item $env:Temp\newfile.txt -ItemType File
创建目录并指定创建目录
New-Item $env:Temp\newdirectory -ItemType Directory
创建注册表键并指定类型为键
New-Item HKLM:\Software\NewKey -ItemType Key
创建临时文件#
New-TemporaryFile
实例:
创建临时文件并写入内容
$file = New-TemporaryFile
Set-Content -Path $file -Value 'Temporary: 10'
Remove-Item $file
获得项属性#
Get-ItemProperty
实例:
获得磁盘目录的属性
Get-ItemProperty D:/
获得注册表的属性
Get-ItemProperty -Path HKCU:\Environment
Get-ItemProperty -Path HKCU:\Environment -Name Path
Get-ItemProperty -Path HKCU:\Environment -Name Path, Temp
设置项属性#
Set-ItemProperty
实例:
设置文件隐藏
Set-ItemProperty -Path "D:/test.txt" -Name Attributes -Value Hidden
设置文件只读
Set-ItemProperty -Path "D:/test.txt" -Name Attributes -Value ReadOnly
Set-ItemProperty .\somefile.txt -Name IsReadOnly -Value $true
还可以直接使用类型的属性
(Get-Item 'somefile.txt').IsReadOnly = $true
设置注册表的属性
Set-ItemProperty -Path HKCU:\Environment -Name NewValue -Value 'New'
移除项的属性#
Remove-ItemProperty
实例:
Remove-ItemProperty -Path HKCU:\Environment -Name NewValue
移动文件#
Move-Item
删除文件#
Remove-Item
实例:
删除东西之前记得带上-Confirm
Remove-Item .\test.txt -Confirm
复制文件#
Copy-Item
重命名#
Rename-Item
输出到控制台#
Write-Output
获得文件内容#
Get-Content
实例:
获得指定文件的内容
Get-Content "D:\test2.csv"
设置文件的内容#
Set-Content
实例:
给指定文件设置指定内容
Set-Content -Value "Panda666.com" -Path "D:/test2.txt"
追加文件内容#
Add-Content
注意:默认会自动换行
注意:如果文件不存在将会自动创建文件
实例:
Add-Content -Value "Panda666" -Path "D:/test.txt"
清除文件的内容#
Clear-Content
实例:
输出内容到文件#
虽然可以使用>重定向,但有时需要设置字符类型等信息
Out-File
实例:
dir | Out-File -FilePath "D:/test.txt" -Encoding utf8
dir | Out-File -FilePath "D:/test.txt" -Encoding ascii -Width 1000
输出股内容到打印机#
Out-Printer
提示:除了可以打印文件外,还可以用于生成PDF文件
实例:
Get-Content "D:/test.txt" | Out-Printer
输出到GridView中进行查看#
Out-GridView
实例:
Get-Service | Out-GridView
Get-Process | Out-GridView
Dir | Out-GridView
不输出任何内容#
Out-Null
压缩和解压#
压缩文件和文件夹
Compress-Archive -Path '源文件文件夹' -DestinationPath '保存位置'
解压文件
Expand-Archive -Path '压缩包位置' -DestinationPath '解压位置'
实例:
Expand-Archive -Path 'D:/test.zip' -DestinationPath 'D:/test'
扩展:
https://blog.csdn.net/ahxdyz/article/details/93534213
转为HTML#
ConvertTo-Html
实例:
转为HTML后输出到文件中
Get-ChildItem | ConvertTo-Html > D:/test.html
转为HTML后输出到标准输出
Get-Service | ConvertTo-Html
转为HTML并指定输出的属性
Get-Process | ConvertTo-Html -Property Name, Id, WorkingSet
转为HTML后指定输出格式
Get-Service | ConvertTo-Html | Out-File -Encoding utf8 "D:service.html"
设置
作者:重庆熊猫
出处:https://www.cnblogs.com/cqpanda/p/16589981.html
版权:本作品采用「不论是否商业使用都不允许转载,否则按3元1字进行收取费用」许可协议进行许可。
本文来自博客园,作者:重庆熊猫,转载请注明原文链接:https://www.cnblogs.com/cqpanda/p/16589981.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类