PowerShell的强大命令集合
PowerShell 中有许多常用命令(称为 cmdlets),这些命令可以帮助你管理和自动化 Windows 操作系统的各个方面。下面是一些常见的 PowerShell cmdlets,以及它们的具体应用示例:
1. Get-Command
- 场景: 当你需要了解 PowerShell 中所有可用的 cmdlets 时。
- 示例:
# 查找所有以 Get- 开头的 cmdlets Get-Command Get-*
2. Get-Help
- 场景: 当你需要查找某个 cmdlet 的使用方法和参数说明时。
- 示例:
# 获取 Get-Process 的帮助文档 Get-Help Get-Process
3. Clear-Host (cls)
- 场景: 当你需要清除控制台屏幕上的内容以便更好地查看输出结果时。
- 示例:
# 清除控制台屏幕 cls
4. Get-ChildItem (dir / ls)
- 场景: 当你需要查看当前目录或指定目录下的文件和子目录列表时。
- 示例:
# 列出当前目录下的所有文件和目录 Get-ChildItem -Path .
5. Set-Location (cd)
- 场景: 当你需要切换到另一个目录继续操作时。
- 示例:
# 切换到用户的文档目录 Set-Location C:\Users\YourName\Documents
6. Get-Location (pwd)
- 场景: 当你需要确认当前所在的目录位置时。
- 示例:
# 显示当前所在目录 Get-Location
7. New-Item
- 场景: 当你需要创建新的文件或目录时。
- 示例:
# 在当前目录下创建一个名为 myFolder 的新目录 New-Item -ItemType Directory -Path .\myFolder
8. Remove-Item (rm)
- 场景: 当你需要删除文件或目录时。
- 示例:
# 删除名为 test.txt 的文件,如果文件不存在则不会抛出错误 Remove-Item C:\temp\test.txt -Force
9. Copy-Item (cp)
- 场景: 当你需要复制文件或目录时。
- 示例:
# 复制文件 test.txt 到另一个目录 Copy-Item C:\source\test.txt C:\destination\
10. Move-Item (mv)
- 场景: 当你需要移动或重命名文件或目录时。
- 示例:
# 将文件 oldname.txt 移动到 newfolder 并重命名为 newname.txt Move-Item C:\temp\oldname.txt C:\temp\newfolder\newname.txt
11. Rename-Item (ren)
- 场景: 当你需要重命名文件或目录时。
- 示例:
# 将文件 oldname.txt 重命名为 newname.txt Rename-Item C:\temp\oldname.txt C:\temp\newname.txt
12. Get-Process
- 场景: 当你需要查看当前系统中正在运行的所有进程时。
- 示例:
# 获取所有运行中的进程 Get-Process
13. Stop-Process
- 场景: 当你需要终止某个或某些进程时。
- 示例:
# 终止所有名为 notepad 的进程 Stop-Process -Name notepad
14. Start-Process
- 场景: 当你需要启动一个新的进程时。
- 示例:
# 启动记事本应用程序 Start-Process notepad
15. Get-Service
- 场景: 当你需要查看系统中所有服务的状态时。
- 示例:
# 获取所有服务的状态 Get-Service
16. Start-Service / Stop-Service / Restart-Service
- 场景: 当你需要启动、停止或重启某个服务时。
- 示例:
# 启动 Print Spooler 服务 Start-Service -Name spooler # 停止 Print Spooler 服务 Stop-Service -Name spooler # 重启 Print Spooler 服务 Restart-Service -Name spooler
17. Get-EventLog
- 场景: 当你需要查看系统日志时。
- 示例:
# 获取应用程序日志中的所有错误条目 Get-EventLog -LogName Application -EntryType Error
18. Add-Type
- 场景: 当你需要加载 DLL 或使用 C# 代码创建 .NET 类时。
- 示例:
# 加载 WindowsBase.dll Add-Type -Path "C:\Windows\System32\WindowsBase.dll"
19. Get-WmiObject
- 场景: 当你需要查询 WMI(Windows Management Instrumentation)信息时。
- 示例:
# 获取操作系统信息 Get-WmiObject -Class Win32_OperatingSystem
20. Set-WmiInstance
- 场景: 当你需要在 WMI 中创建或修改实例时。
- 示例:
# 创建一个新的进程实例 $obj = New-Object -ComObject WbemScripting.SWbemLocator $connection = $obj.ConnectServer("localhost","root\CIMV2") $class = $connection.Get("Win32_Process") $instance = $class.CreateInstance() $instance.Name = "notepad.exe" $instance.Path_ = "%windir%\system32\notepad.exe" $instance.Create()
21. Invoke-WebRequest (Invoke-RestMethod)
- 场景: 当你需要发送 HTTP 请求获取 Web 数据时。
- 示例:
# 获取 GitHub 上最新发布的版本信息 Invoke-WebRequest -Uri "https://api.github.com/repos/powershell/powershell/releases/latest"
22. Import-Csv / Export-Csv
- 场景: 当你需要从 CSV 文件导入数据或将数据导出为 CSV 文件时。
- 示例:
# 从 CSV 文件导入数据 $data = Import-Csv -Path "C:\temp\data.csv" # 将数据导出为 CSV 文件 $data | Export-Csv -Path "C:\temp\output.csv" -NoTypeInformation
23. Get-Content (gc) / Set-Content (sc) / Add-Content (ac)
- 场景: 当你需要读取、写入或追加文件内容时。
- 示例:
# 读取文件内容 $content = Get-Content -Path "C:\temp\input.txt" # 写入文件内容 "Hello, world!" | Set-Content -Path "C:\temp\output.txt" # 追加内容到文件 "Another line" | Add-Content -Path "C:\temp\output.txt"
24. Select-String
- 场景: 当你需要在文件中搜索字符串时。
- 示例:
# 在日志文件中搜索含有 "error" 的行 Select-String -Path "C:\temp\log.txt" -Pattern "error"
25. Write-Host / Write-Output
- 场景: 当你需要向主机窗口输出文本时。
- 示例:
# 输出 Hello, world! Write-Host "Hello, world!"
26. Where-Object (where)
- 场景: 当你需要对对象集合应用过滤条件时。
- 示例:
# 获取 CPU 占用率大于 10% 的所有进程 Get-Process | Where-Object { $_.CPU -gt 10 }
27. Group-Object
- 场景: 当你需要根据属性对对象进行分组时。
- 示例:
# 按主模块的文件名分组进程 Get-Process | Group-Object -Property MainModule.FileName
28. Sort-Object (sort)
- 场景: 当你需要对对象进行排序时。
- 示例:
# 按 CPU 占用率降序排列所有进程 Get-Process | Sort-Object -Property CPU -Descending
29. Measure-Command
- 场景: 当你需要测量命令执行所需的时间时。
- 示例:
# 测量获取所有进程所需的时间 Measure-Command -Expression { Get-Process }
30. Compare-Object
- 场景: 当你需要比较两个对象集合的差异时。
- 示例:
# 比较两个数字列表的不同之处 $list1 = 1..5 $list2 = 2..6 Compare-Object -ReferenceObject $list1 -DifferenceObject $list2
这些命令在不同的场景下都有其特定的应用价值,可以根据实际需求灵活运用。
当然,这些 cmdlets 只是冰山一角,PowerShell 的功能远不止于此。通过组合这些基本命令,你可以编写复杂的脚本来完成各种管理任务。此外,PowerShell 社区还提供了大量的模块,这些模块包含了更多的 cmdlets,可以扩展 PowerShell 的功能。