PowerShell 常用命令

常用命令

Remove-Item——删除指定的项目

Remove-Item
      [-Path] <String[]>
      [-Filter <String>]
      [-Include <String[]>]
      [-Exclude <String[]>]
      [-Recurse]
      [-Force]
      [-Credential <PSCredential>]
      [-WhatIf]
      [-Confirm]
      [-Stream <String[]>]
      [<CommonParameters>]

使用示例:

Remove-Item C:\Test\*.*
Remove-Item * -Include *.doc -Exclude *1*
# 删除隐藏文件和只读文件
Remove-Item -Path C:\Test\hidden-RO-file.txt -Force
# 递归删除子文件夹中的文件
Get-ChildItem * -Include *.csv -Recurse | Remove-Item
# 递归删除注册表项
Remove-Item HKLM:\Software\MyCompany\OldApp -Recurse
# 删除带有特殊字符的文件
Get-ChildItem | Where-Object Name -Like '*`[*' | ForEach-Object { Remove-Item -LiteralPath $_.Name }
# 删除备用数据流
Remove-Item C:\Test\Copy-Script.ps1 -Stream Zone.Identifier

官方文档


Rename-Item——重命名项目

Rename-Item
      [-Path] <String>
      [-NewName] <String>
      [-Force]
      [-PassThru]
      [-Credential <PSCredential>]
      [-WhatIf]
      [-Confirm] 
      [<CommonParameters>]

使用示例:

Rename-Item -Path "c:\logfiles\daily_file.txt" -NewName "monday_file.txt"
# 重命名和移动文件
Rename-Item -Path "project.txt" -NewName "d:\archive\old-project.txt"
# 重命名注册表项
Rename-Item -Path "HKLM:\Software\MyCompany\Advertising" -NewName "Marketing"
# 重命名多个文件
Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace '.txt','.log' }

官方文档


Get-ChildItem——在一个或多个指定位置获取项目和子项目

Get-ChildItem
   [[-Path] <string[]>]
   [[-Filter] <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Recurse]
   [-Depth <uint32>]
   [-Force]
   [-Name]
   [-Attributes <FlagsExpression[FileAttributes]>]
   [-FollowSymlink]
   [-Directory]
   [-File]
   [-Hidden]
   [-ReadOnly]
   [-System]
   [<CommonParameters>]

使用示例:

Get-ChildItem -Path C:\Test
Get-ChildItem -Path C:\Test -Name
Get-ChildItem -Path C:\Test\*.txt -Recurse -Force
Get-ChildItem -Path C:\Test\* -Include *.txt
Get-ChildItem -Path C:\Test\Logs\* -Exclude A*
# 从注册表配置单元获取注册表项
Get-ChildItem -Path HKLM:\HARDWARE -Exclude D*
# 获取具有代码签名权限的所有证书
Get-ChildItem -Path Cert:\* -Recurse -CodeSigningCert
# 使用深度参数获取项目
Get-ChildItem -Path C:\Parent -Depth 2
# 获取硬链接信息
Get-ChildItem -Path C:\PathContainingHardLink | Format-Table -View childrenWithHardLink
# 非Windows操作系统的输出
Get-ChildItem /etc/r*

官方文档


相关链接

官方文档首页
官方API

posted @ 2020-12-19 10:57  qianbuhan  阅读(667)  评论(0)    收藏  举报