Skip to content

Commit 3662bb7

Browse files
committed
doc: update docs/powershell.md #845
1 parent 2c6244b commit 3662bb7

File tree

1 file changed

+56
-48
lines changed

1 file changed

+56
-48
lines changed

docs/powershell.md

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
## 基本命令
1+
PowerShell 备忘清单
2+
===
23

3-
### 辅助命令
4+
一种强大的命令行界面和脚本语言,主要用于自动化任务和配置管理,特别适合系统管理员和 IT 专业人士。以下是 PowerShell 常用命令的备忘清单,可帮助快速参考常用操作。
5+
6+
常用操作
7+
---
48

5-
**_Powershell 的命令遵循动词-名词格式_**
9+
### 辅助命令
610

7-
一些常见的动词:
11+
**_PowerShell 的命令遵循动词-名词格式_** 一些常见的动词:
812

913
| 动词 | 描述 |
1014
| ------- | ------------------------ |
@@ -22,86 +26,85 @@
2226

2327
列出可用模块
2428

25-
```powershell
29+
```PowerShell
2630
Get-Module --ListAvailable
2731
```
2832

2933
列出可用的 cmdlet 和函数
3034

31-
```powershell
35+
```PowerShell
3236
Get-Command -Module ActiveDirectory
3337
```
3438

39+
列出别名及其对应的 cmdlet 名称
40+
41+
```PowerShell
42+
Get-Alias | Select-Object Name, Definition
43+
```
44+
3545
获取帮助
3646

37-
```powershell
47+
```PowerShell
3848
Get-Help <cmd>
3949
Get-Help <cmd> -Examples
4050
Get-Help -Name Get-Process -Parameter Id
4151
```
4252

43-
列出别名及其对应的 cmdlet 名称
44-
45-
46-
```powershell
47-
Get-Alias | Select-Object Name, Definition
48-
```
49-
5053
**Get-Member:** 显示对象的属性和方法
5154

52-
```powershell
55+
```PowerShell
5356
Get-Process | Get-Member
5457
```
5558

5659
### 对象操作
5760

5861
**Select-Object:** 选择对象的特定属性或自定义其显示
5962

60-
```powershell
63+
```PowerShell
6164
Get-Process | Select-Object Name, CPU
6265
```
6366

6467
**Where-Object:** 根据指定条件过滤对象
6568

66-
```powershell
69+
```PowerShell
6770
Get-Service | Where-Object { $PSItem.Status -eq 'Running' }
6871
#OR
6972
Get-Service | ? { $_.Status -eq 'Running' }
7073
```
7174

7275
**Measure-Object:** 计算对象属性的统计信息,如总和、平均值和计数
7376

74-
```powershell
77+
```PowerShell
7578
Get-Process | Measure-Object -Property WorkingSet -Sum
7679
```
7780

7881
**ForEach-Object:** 对集合中的每个对象执行操作(注意:以下命令将为当前目录中的文件/文件夹添加前缀)
7982

80-
```powershell
83+
```PowerShell
8184
Get-ChildItem | ForEach-Object { Rename-Item $_ -NewName "Prefix_$_" }
8285
```
8386

8487
**Sort-Object:** 按指定属性对对象进行排序
8588

86-
```powershell
89+
```PowerShell
8790
Get-ChildItem | Sort-Object Length -Descending
8891
```
8992

9093
**Format-Table:** 将输出格式化为带有指定列的表格
9194

92-
```powershell
95+
```PowerShell
9396
Get-Service | Format-Table -AutoSize # ft alias
9497
```
9598

9699
**Format-List:** 将输出格式化为属性和值的列表
97100

98-
```powershell
101+
```PowerShell
99102
Get-Process | Format-List -Property Name, CPU # fl alias
100103
```
101104

102-
### 文件系统
105+
### 文件系统
103106

104-
```powershell
107+
```PowerShell
105108
New-Item -path file.txt -type 'file' -value 'contents'
106109
New-Item -path file.txt -type 'dir'
107110
Copy-Item <src> -destination <dest>
@@ -120,9 +123,12 @@ Get-Process | Export-Csv -Path "processes.csv" # 输出到 CSV
120123
$data = Import-Csv -Path "data.csv" # 从 CSV 导入
121124
```
122125

123-
## 系统管理
126+
系统管理
127+
---
128+
129+
### 获取信息
124130

125-
```powershell
131+
```PowerShell
126132
# 获取 BIOS 信息
127133
Get-CimInstance -ClassName Win32_BIOS
128134
# 获取本地连接的物理磁盘设备信息
@@ -133,7 +139,11 @@ Get-CimInstance -ClassName Win32_PhysicalMemory
133139
Get-CimInstance -ClassName Win32_NetworkAdapter
134140
# 获取安装的图形/显卡(GPU)信息
135141
Get-CimInstance -ClassName Win32_VideoController
142+
```
143+
144+
### 命名空间 & 类
136145

146+
```PowerShell
137147
# 列出所有类名
138148
Get-CimClass | Select-Object -ExpandProperty CimClassName
139149
# 探索 root\cimv2 命名空间中的各种 WMI 类
@@ -144,7 +154,7 @@ Get-CimInstance -Namespace root -ClassName __NAMESPACE
144154

145155
### 网络管理
146156

147-
```powershell
157+
```PowerShell
148158
# 测试与远程主机的网络连接
149159
Test-Connection -ComputerName google.com
150160
@@ -164,7 +174,7 @@ Test-NetConnection google.com -Port 80
164174

165175
### 用户和组管理
166176

167-
```powershell
177+
```PowerShell
168178
# 获取本地用户账户信息
169179
Get-LocalUser
170180
@@ -183,7 +193,7 @@ Add-LocalGroupMember -Group Administrators -Member UserToAdd
183193

184194
### 安全性和权限
185195

186-
```powershell
196+
```PowerShell
187197
# 获取文件/目录的访问控制列表
188198
Get-Acl C:\Path\To\File.txt
189199
@@ -193,7 +203,7 @@ Set-Acl -Path C:\Path\To\File.txt -AclObject $aclObject
193203

194204
### 注册表管理
195205

196-
```powershell
206+
```PowerShell
197207
# 获取注册表键值
198208
Get-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" | Select DisplayName, DisplayVersion
199209
@@ -212,11 +222,11 @@ Test-Path "HKLM:\Software\MyApp"
212222

213223
## 脚本
214224

215-
### 变量
225+
### 变量
216226

217227
初始化变量,指定或不指定类型:
218228

219-
```powershell
229+
```PowerShell
220230
$var = 0
221231
[int] $var = 'Trevor' # (抛出异常)
222232
[string] $var = 'Trevor' # (不会抛出异常)
@@ -234,7 +244,7 @@ $dict = @{k1 = 'test'; k2 = 'best'}
234244

235245
变量命令
236246

237-
```Powershell
247+
```PowerShell
238248
New-Variable -Name FirstName -Value Trevor
239249
New-Variable FirstName -Value Trevor -Option <ReadOnly/Constant>
240250
@@ -251,7 +261,7 @@ Remove-Variable -Name firstname -Force
251261

252262
### 运算符
253263

254-
```powershell
264+
```PowerShell
255265
# 运算符
256266
# (a <op> b)
257267
@@ -284,7 +294,7 @@ $regex.Matches('this is test').Value
284294

285295
#### 输入输出操作
286296

287-
```powershell
297+
```PowerShell
288298
"This displays a string"
289299
290300
Write-Host "color" -ForegroundColor Red
@@ -298,12 +308,12 @@ Clear-Host
298308

299309
#### 流控制
300310

301-
```powershell
311+
```PowerShell
302312
IF(<#Condition#>){
303313
<#Commands#>}ELSEIF(){}ELSE{}
304314
305315
Switch($var){
306-
"val1"{<#Commands#>; break}
316+
"val1"{<#Commands#>; break}
307317
"val2"{<#Commands#>; break}
308318
}
309319
@@ -321,23 +331,23 @@ Do{}While()
321331

322332
#### 示例 1
323333

324-
```powershell
334+
```PowerShell
325335
function funcname{
326336
327337
[CmdletBinding()]
328-
param(
329-
[Parameter(Mandatory)]
330-
[String]$user
331-
)
332-
Write-Host "welcome " $user
338+
param(
339+
[Parameter(Mandatory)]
340+
[String]$user
341+
)
342+
Write-Host "welcome " $user
333343
return "value"
334344
}
335345
$var = funcname -user pcb
336346
```
337347

338348
#### 示例 2
339349

340-
```powershell
350+
```PowerShell
341351
function Get-EvenNumbers {
342352
[CmdletBinding()]
343353
param (
@@ -358,7 +368,7 @@ function Get-EvenNumbers {
358368

359369
#### 模块
360370

361-
```powershell
371+
```PowerShell
362372
# PowerShell 在路径中查找模块
363373
$env:PSModulePath
364374
@@ -381,11 +391,9 @@ New-Module -Name trevor -ScriptBlock {
381391

382392
### 注意
383393

384-
385394
- 在大多数语言中,转义字符是反斜杠 **\\**,而在 PowerShell 中是反引号 **`**
386395

387-
388396
## 参考
389397

390-
- [Microsoft Powershell](https://learn.microsoft.com/en-us/powershell/scripting/samples/sample-scripts-for-administration?view=powershell-7.3) _(learn.microsoft.com)_
398+
- [Microsoft PowerShell](https://learn.microsoft.com/en-us/powershell/scripting/samples/sample-scripts-for-administration?view=powershell-7.3) _(learn.microsoft.com)_
391399
- [cheatsheets](https://cheatsheets.zip/powershell)

0 commit comments

Comments
 (0)