doc: update docs/powershell.md #845

This commit is contained in:
jaywcjlove 2024-10-29 20:09:26 +08:00
parent 2c6244b6f8
commit 3662bb703c

View File

@ -1,10 +1,14 @@
## 基本命令 PowerShell 备忘清单
===
一种强大的命令行界面和脚本语言,主要用于自动化任务和配置管理,特别适合系统管理员和 IT 专业人士。以下是 PowerShell 常用命令的备忘清单,可帮助快速参考常用操作。
常用操作
---
### 辅助命令 ### 辅助命令
**_Powershell 的命令遵循动词-名词格式_** **_PowerShell 的命令遵循动词-名词格式_** 一些常见的动词:
一些常见的动词:
| 动词 | 描述 | | 动词 | 描述 |
| ------- | ------------------------ | | ------- | ------------------------ |
@ -22,34 +26,33 @@
列出可用模块 列出可用模块
```powershell ```PowerShell
Get-Module --ListAvailable Get-Module --ListAvailable
``` ```
列出可用的 cmdlet 和函数 列出可用的 cmdlet 和函数
```powershell ```PowerShell
Get-Command -Module ActiveDirectory Get-Command -Module ActiveDirectory
``` ```
列出别名及其对应的 cmdlet 名称
```PowerShell
Get-Alias | Select-Object Name, Definition
```
获取帮助 获取帮助
```powershell ```PowerShell
Get-Help <cmd> Get-Help <cmd>
Get-Help <cmd> -Examples Get-Help <cmd> -Examples
Get-Help -Name Get-Process -Parameter Id Get-Help -Name Get-Process -Parameter Id
``` ```
列出别名及其对应的 cmdlet 名称
```powershell
Get-Alias | Select-Object Name, Definition
```
**Get-Member:** 显示对象的属性和方法 **Get-Member:** 显示对象的属性和方法
```powershell ```PowerShell
Get-Process | Get-Member Get-Process | Get-Member
``` ```
@ -57,13 +60,13 @@ Get-Process | Get-Member
**Select-Object:** 选择对象的特定属性或自定义其显示 **Select-Object:** 选择对象的特定属性或自定义其显示
```powershell ```PowerShell
Get-Process | Select-Object Name, CPU Get-Process | Select-Object Name, CPU
``` ```
**Where-Object:** 根据指定条件过滤对象 **Where-Object:** 根据指定条件过滤对象
```powershell ```PowerShell
Get-Service | Where-Object { $PSItem.Status -eq 'Running' } Get-Service | Where-Object { $PSItem.Status -eq 'Running' }
#OR #OR
Get-Service | ? { $_.Status -eq 'Running' } Get-Service | ? { $_.Status -eq 'Running' }
@ -71,37 +74,37 @@ Get-Service | ? { $_.Status -eq 'Running' }
**Measure-Object:** 计算对象属性的统计信息,如总和、平均值和计数 **Measure-Object:** 计算对象属性的统计信息,如总和、平均值和计数
```powershell ```PowerShell
Get-Process | Measure-Object -Property WorkingSet -Sum Get-Process | Measure-Object -Property WorkingSet -Sum
``` ```
**ForEach-Object:** 对集合中的每个对象执行操作(注意:以下命令将为当前目录中的文件/文件夹添加前缀) **ForEach-Object:** 对集合中的每个对象执行操作(注意:以下命令将为当前目录中的文件/文件夹添加前缀)
```powershell ```PowerShell
Get-ChildItem | ForEach-Object { Rename-Item $_ -NewName "Prefix_$_" } Get-ChildItem | ForEach-Object { Rename-Item $_ -NewName "Prefix_$_" }
``` ```
**Sort-Object:** 按指定属性对对象进行排序 **Sort-Object:** 按指定属性对对象进行排序
```powershell ```PowerShell
Get-ChildItem | Sort-Object Length -Descending Get-ChildItem | Sort-Object Length -Descending
``` ```
**Format-Table:** 将输出格式化为带有指定列的表格 **Format-Table:** 将输出格式化为带有指定列的表格
```powershell ```PowerShell
Get-Service | Format-Table -AutoSize # ft alias Get-Service | Format-Table -AutoSize # ft alias
``` ```
**Format-List:** 将输出格式化为属性和值的列表 **Format-List:** 将输出格式化为属性和值的列表
```powershell ```PowerShell
Get-Process | Format-List -Property Name, CPU # fl alias Get-Process | Format-List -Property Name, CPU # fl alias
``` ```
### 文件系统 ### 文件系统
```powershell ```PowerShell
New-Item -path file.txt -type 'file' -value 'contents' New-Item -path file.txt -type 'file' -value 'contents'
New-Item -path file.txt -type 'dir' New-Item -path file.txt -type 'dir'
Copy-Item <src> -destination <dest> Copy-Item <src> -destination <dest>
@ -120,9 +123,12 @@ Get-Process | Export-Csv -Path "processes.csv" # 输出到 CSV
$data = Import-Csv -Path "data.csv" # 从 CSV 导入 $data = Import-Csv -Path "data.csv" # 从 CSV 导入
``` ```
## 系统管理 系统管理
---
```powershell ### 获取信息
```PowerShell
# 获取 BIOS 信息 # 获取 BIOS 信息
Get-CimInstance -ClassName Win32_BIOS Get-CimInstance -ClassName Win32_BIOS
# 获取本地连接的物理磁盘设备信息 # 获取本地连接的物理磁盘设备信息
@ -133,7 +139,11 @@ Get-CimInstance -ClassName Win32_PhysicalMemory
Get-CimInstance -ClassName Win32_NetworkAdapter Get-CimInstance -ClassName Win32_NetworkAdapter
# 获取安装的图形/显卡GPU信息 # 获取安装的图形/显卡GPU信息
Get-CimInstance -ClassName Win32_VideoController Get-CimInstance -ClassName Win32_VideoController
```
### 命名空间 &
```PowerShell
# 列出所有类名 # 列出所有类名
Get-CimClass | Select-Object -ExpandProperty CimClassName Get-CimClass | Select-Object -ExpandProperty CimClassName
# 探索 root\cimv2 命名空间中的各种 WMI 类 # 探索 root\cimv2 命名空间中的各种 WMI 类
@ -144,7 +154,7 @@ Get-CimInstance -Namespace root -ClassName __NAMESPACE
### 网络管理 ### 网络管理
```powershell ```PowerShell
# 测试与远程主机的网络连接 # 测试与远程主机的网络连接
Test-Connection -ComputerName google.com Test-Connection -ComputerName google.com
@ -164,7 +174,7 @@ Test-NetConnection google.com -Port 80
### 用户和组管理 ### 用户和组管理
```powershell ```PowerShell
# 获取本地用户账户信息 # 获取本地用户账户信息
Get-LocalUser Get-LocalUser
@ -183,7 +193,7 @@ Add-LocalGroupMember -Group Administrators -Member UserToAdd
### 安全性和权限 ### 安全性和权限
```powershell ```PowerShell
# 获取文件/目录的访问控制列表 # 获取文件/目录的访问控制列表
Get-Acl C:\Path\To\File.txt Get-Acl C:\Path\To\File.txt
@ -193,7 +203,7 @@ Set-Acl -Path C:\Path\To\File.txt -AclObject $aclObject
### 注册表管理 ### 注册表管理
```powershell ```PowerShell
# 获取注册表键值 # 获取注册表键值
Get-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" | Select DisplayName, DisplayVersion Get-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" | Select DisplayName, DisplayVersion
@ -216,7 +226,7 @@ Test-Path "HKLM:\Software\MyApp"
初始化变量,指定或不指定类型: 初始化变量,指定或不指定类型:
```powershell ```PowerShell
$var = 0 $var = 0
[int] $var = 'Trevor' # (抛出异常) [int] $var = 'Trevor' # (抛出异常)
[string] $var = 'Trevor' # (不会抛出异常) [string] $var = 'Trevor' # (不会抛出异常)
@ -234,7 +244,7 @@ $dict = @{k1 = 'test'; k2 = 'best'}
变量命令 变量命令
```Powershell ```PowerShell
New-Variable -Name FirstName -Value Trevor New-Variable -Name FirstName -Value Trevor
New-Variable FirstName -Value Trevor -Option <ReadOnly/Constant> New-Variable FirstName -Value Trevor -Option <ReadOnly/Constant>
@ -251,7 +261,7 @@ Remove-Variable -Name firstname -Force
### 运算符 ### 运算符
```powershell ```PowerShell
# 运算符 # 运算符
# (a <op> b) # (a <op> b)
@ -284,7 +294,7 @@ $regex.Matches('this is test').Value
#### 输入输出操作 #### 输入输出操作
```powershell ```PowerShell
"This displays a string" "This displays a string"
Write-Host "color" -ForegroundColor Red Write-Host "color" -ForegroundColor Red
@ -298,7 +308,7 @@ Clear-Host
#### 流控制 #### 流控制
```powershell ```PowerShell
IF(<#Condition#>){ IF(<#Condition#>){
<#Commands#>}ELSEIF(){}ELSE{} <#Commands#>}ELSEIF(){}ELSE{}
@ -321,7 +331,7 @@ Do{}While()
#### 示例 1 #### 示例 1
```powershell ```PowerShell
function funcname{ function funcname{
[CmdletBinding()] [CmdletBinding()]
@ -337,7 +347,7 @@ $var = funcname -user pcb
#### 示例 2 #### 示例 2
```powershell ```PowerShell
function Get-EvenNumbers { function Get-EvenNumbers {
[CmdletBinding()] [CmdletBinding()]
param ( param (
@ -358,7 +368,7 @@ function Get-EvenNumbers {
#### 模块 #### 模块
```powershell ```PowerShell
# PowerShell 在路径中查找模块 # PowerShell 在路径中查找模块
$env:PSModulePath $env:PSModulePath
@ -381,11 +391,9 @@ New-Module -Name trevor -ScriptBlock {
### 注意 ### 注意
- 在大多数语言中,转义字符是反斜杠 **\\**,而在 PowerShell 中是反引号 **`** - 在大多数语言中,转义字符是反斜杠 **\\**,而在 PowerShell 中是反引号 **`**
## 参考 ## 参考
- [Microsoft Powershell](https://learn.microsoft.com/en-us/powershell/scripting/samples/sample-scripts-for-administration?view=powershell-7.3) _(learn.microsoft.com)_ - [Microsoft PowerShell](https://learn.microsoft.com/en-us/powershell/scripting/samples/sample-scripts-for-administration?view=powershell-7.3) _(learn.microsoft.com)_
- [cheatsheets](https://cheatsheets.zip/powershell) - [cheatsheets](https://cheatsheets.zip/powershell)