ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

PowerShell教程 - 文件系统管理(File System Management)

2022-08-24 07:30:51  阅读:154  来源: 互联网

标签:文件 Management Get System Item 实例 File Path Set


更新记录
转载请注明出处。
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"

设置标签内的内容</p> <pre><code class="language-powershell">Get-Printer | ConvertTo-Html -Title "Panda666" </code></pre> <p>设置<head>标签内的内容</p> <pre><code class="language-powershell">Get-Printer | ConvertTo-Html -Head "<title>Panda666</title>" </code></pre> <p>设置添加到<body>内开头的内容</p> <pre><code class="language-powershell">Get-Printer | ConvertTo-Html -Body "Panda666" </code></pre> <p>设置字符编码</p> <pre><code class="language-powershell">Get-Printer | ConvertTo-Html -Charset "UTF-8" </code></pre> <p>使用预定义的内容填充到HTML文档中</p> <pre><code class="language-powershell">ConvertTo-Html -Body $body -Title $title | Set-Content report.html </code></pre> <h2 id="转为xml">转为XML</h2> <pre><code class="language-powershell">ConvertTo-Xml </code></pre> <p>实例:</p> <pre><code class="language-powershell">Get-ChildItem | ConvertTo-Xml > D:/test.xml </code></pre> <h2 id="导出xml">导出XML</h2> <pre><code class="language-powershell">Export-Clixml </code></pre> <p>注意:该命令会保存额外的类型信息,可以用于类型的导入和导出</p> <pre><code class="language-powershell">[PSCustomObject]@{ Number = 1 Decimal = 2.3 String = 'Hello world' } | Export-Clixml .\object.xml </code></pre> <h2 id="导入xml">导入XML</h2> <pre><code class="language-powershell">Import-Clixml </code></pre> <p>实例:<br> 导入之前导出的xml并转为对象</p> <pre><code class="language-powershell">$object = Import-Clixml .\object.xml </code></pre> <h2 id="导入csv">导入CSV</h2> <pre><code class="language-powershell">Import-Csv </code></pre> <p>实例:</p> <pre><code class="language-powershell">Import-Csv D:/test.csv </code></pre> <p>指定分隔符</p> <pre><code class="language-powershell">Import-Csv TabDelimitedFile.tsv -Delimiter `t </code></pre> <h2 id="导出csv">导出CSV</h2> <pre><code class="language-powershell">Export-Csv </code></pre> <p>实例:</p> <pre><code class="language-powershell">Get-Process | Export-Csv D:/test.csv </code></pre> <p>指定分隔符</p> <pre><code class="language-powershell">Get-Service | Export-Csv -Encoding UTF8 -Delimiter '|' -Path "D:/t.txt" </code></pre> <p>去除头部文件类型信息</p> <pre><code class="language-powershell">Get-Service | Export-Csv -Encoding UTF8 -Path "D:/t.txt" -NoTypeInformation </code></pre> <p>带确认提示</p> <pre><code class="language-powershell">Get-Service | Export-Csv -Path "D:/t.txt" -NoTypeInformation -Confirm </code></pre> <p>追加</p> <pre><code class="language-powershell">Export-Csv .\Processes.csv -Append </code></pre> <h2 id="转为csv">转为CSV</h2> <pre><code class="language-powershell">ConvertTo-Csv </code></pre> <p>实例:</p> <pre><code class="language-powershell">Get-ChildItem | ConvertTo-Csv > D:/test.csv </code></pre> <h2 id="转为json">转为JSON</h2> <pre><code class="language-powershell">ConvertTo-Json </code></pre> <p>实例:</p> <pre><code class="language-powershell">Get-ChildItem | ConvertTo-Json > D:/test.json </code></pre> <h2 id="从json转为对象">从JSON转为对象</h2> <pre><code class="language-powershell">ConvertFrom-Json </code></pre> <p>实例:</p> <pre><code class="language-powershell">'{ "Property": "Value" }' | ConvertFrom-Json </code></pre> <h2 id="格式化表格">格式化表格</h2> <pre><code class="language-powershell">Format-Table </code></pre> <p>实例:<br> 自动化表格尺寸</p> <pre><code class="language-powershell">Get-Service | Format-Table -autoSize </code></pre> <p>指定显示的属性<br> 注意:属性名称不区分大小写</p> <pre><code class="language-powershell">Get-Service | Format-Table -Property name </code></pre> <p>截断过长的文本</p> <pre><code class="language-powershell">Get-Service | Format-Table -Wrap </code></pre> <h2 id="格式化列表">格式化列表</h2> <pre><code class="language-powershell">Format-List </code></pre> <p>实例:<br> 指定显示的属性<br> 注意:属性名称不区分大小写</p> <pre><code class="language-powershell">Get-Service | Format-List -Property name </code></pre> <h2 id="格式化宽列表">格式化宽列表</h2> <pre><code class="language-powershell">Format-Wide </code></pre> <p>实例:<br> 指定显示的属性<br> 注意:宽列表中-property只可以显示一个属性</p> <pre><code class="language-powershell">Get-Service | Format-Wide -Property DisplayName </code></pre> <p>指定列数</p> <pre><code class="language-powershell">Get-Service | Format-Wide -Column 2 </code></pre> <h2 id="转为string字符串">转为String字符串</h2> <pre><code class="language-powershell">Out-String </code></pre> <p>实例:</p> <pre><code class="language-powershell">"panda666" | Out-String </code></pre> <h2 id="获得没重复值的序列">获得没重复值的序列</h2> <pre><code class="language-powershell">Get-Unique </code></pre> <p>常用于在多个重复元素中,排除重复元素</p> <p>实例:</p> <pre><code class="language-powershell">1,1,2,3,4 | Get-Unique # 1 2 3 4 </code></pre> <h2 id="调用命令">调用命令</h2> <pre><code class="language-powershell">Invoke-Item </code></pre> <p>open or execute an object using the default settings<br> 调用命令在不同的情况下,表现不同<br> 别名:ii</p> <p>实例;<br> 打开当前目录</p> <pre><code class="language-powershell">Invoke-Item . </code></pre> <p>在默认的编辑器中打开脚本</p> <pre><code class="language-powershell">Invoke-Item test.ps1 </code></pre> <p>调用cmd</p> <pre><code class="language-powershell">Invoke-Item $env:windir\system32\cmd.exe </code></pre> <p>打开证书管理界面</p> <pre><code class="language-powershell">Invoke-Item Cert: </code></pre> <h2 id="注册新的psdrive驱动">注册新的PSDrive驱动</h2> <pre><code class="language-powershell">New-PSDrive </code></pre> <p>实例:</p> <pre><code class="language-powershell">New-PSDrive X -PSProvider FileSystem -Root \\Server\Share New-PSDrive HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT </code></pre> <h2 id="移除现有的驱动">移除现有的驱动</h2> <pre><code class="language-powershell">Remove-PSDrive </code></pre> <p>实例:</p> <h1 id="windows权限windows-permissions">Windows权限(Windows permissions)</h1> <h2 id="windows权限说明">Windows权限说明</h2> <p>通常使用Get-Acl 和 Set-Acl命令</p> <pre><code class="language-powershell">Access Control List (ACL) </code></pre> <h2 id="获得权限">获得权限</h2> <pre><code class="language-powershell">Get-Acl </code></pre> <p>实例:</p> <p>获得文件夹的拥有者</p> <pre><code class="language-powershell">Get-Acl 'F:\Vmware' | Select-Object Owner </code></pre> <p>获得权限的详细</p> <pre><code class="language-powershell">Get-Acl F:\Vmware -Audit | Format-List </code></pre> <h2 id="设置权限">设置权限</h2> <pre><code class="language-powershell">Set-Acl </code></pre> <p>实例:</p> <pre><code class="language-powershell">$acl = Get-Acl F:/Vmware $acl.SetOwner([System.Security.Principal.NTAccount]'Administrator') Set-Acl F:/Vmware -AclObject $acl </code></pre>

标签:文件,Management,Get,System,Item,实例,File,Path,Set
来源: https://www.cnblogs.com/cqpanda/p/16589981.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有