1
- ## 基本命令
1
+ PowerShell 备忘清单
2
+ ===
2
3
3
- ### 辅助命令
4
+ 一种强大的命令行界面和脚本语言,主要用于自动化任务和配置管理,特别适合系统管理员和 IT 专业人士。以下是 PowerShell 常用命令的备忘清单,可帮助快速参考常用操作。
5
+
6
+ 常用操作
7
+ ---
4
8
5
- ** _ Powershell 的命令遵循动词-名词格式 _ **
9
+ ### 辅助命令
6
10
7
- 一些常见的动词:
11
+ ** _ PowerShell 的命令遵循动词-名词格式 _ ** 一些常见的动词:
8
12
9
13
| 动词 | 描述 |
10
14
| ------- | ------------------------ |
22
26
23
27
列出可用模块
24
28
25
- ``` powershell
29
+ ``` PowerShell
26
30
Get-Module --ListAvailable
27
31
```
28
32
29
33
列出可用的 cmdlet 和函数
30
34
31
- ``` powershell
35
+ ``` PowerShell
32
36
Get-Command -Module ActiveDirectory
33
37
```
34
38
39
+ 列出别名及其对应的 cmdlet 名称
40
+
41
+ ``` PowerShell
42
+ Get-Alias | Select-Object Name, Definition
43
+ ```
44
+
35
45
获取帮助
36
46
37
- ``` powershell
47
+ ``` PowerShell
38
48
Get-Help <cmd>
39
49
Get-Help <cmd> -Examples
40
50
Get-Help -Name Get-Process -Parameter Id
41
51
```
42
52
43
- 列出别名及其对应的 cmdlet 名称
44
-
45
-
46
- ``` powershell
47
- Get-Alias | Select-Object Name, Definition
48
- ```
49
-
50
53
** Get-Member:** 显示对象的属性和方法
51
54
52
- ``` powershell
55
+ ``` PowerShell
53
56
Get-Process | Get-Member
54
57
```
55
58
56
59
### 对象操作
57
60
58
61
** Select-Object:** 选择对象的特定属性或自定义其显示
59
62
60
- ``` powershell
63
+ ``` PowerShell
61
64
Get-Process | Select-Object Name, CPU
62
65
```
63
66
64
67
** Where-Object:** 根据指定条件过滤对象
65
68
66
- ``` powershell
69
+ ``` PowerShell
67
70
Get-Service | Where-Object { $PSItem.Status -eq 'Running' }
68
71
#OR
69
72
Get-Service | ? { $_.Status -eq 'Running' }
70
73
```
71
74
72
75
** Measure-Object:** 计算对象属性的统计信息,如总和、平均值和计数
73
76
74
- ``` powershell
77
+ ``` PowerShell
75
78
Get-Process | Measure-Object -Property WorkingSet -Sum
76
79
```
77
80
78
81
** ForEach-Object:** 对集合中的每个对象执行操作(注意:以下命令将为当前目录中的文件/文件夹添加前缀)
79
82
80
- ``` powershell
83
+ ``` PowerShell
81
84
Get-ChildItem | ForEach-Object { Rename-Item $_ -NewName "Prefix_$_" }
82
85
```
83
86
84
87
** Sort-Object:** 按指定属性对对象进行排序
85
88
86
- ``` powershell
89
+ ``` PowerShell
87
90
Get-ChildItem | Sort-Object Length -Descending
88
91
```
89
92
90
93
** Format-Table:** 将输出格式化为带有指定列的表格
91
94
92
- ``` powershell
95
+ ``` PowerShell
93
96
Get-Service | Format-Table -AutoSize # ft alias
94
97
```
95
98
96
99
** Format-List:** 将输出格式化为属性和值的列表
97
100
98
- ``` powershell
101
+ ``` PowerShell
99
102
Get-Process | Format-List -Property Name, CPU # fl alias
100
103
```
101
104
102
- ### 文件系统
105
+ ### 文件系统
103
106
104
- ``` powershell
107
+ ``` PowerShell
105
108
New-Item -path file.txt -type 'file' -value 'contents'
106
109
New-Item -path file.txt -type 'dir'
107
110
Copy-Item <src> -destination <dest>
@@ -120,9 +123,12 @@ Get-Process | Export-Csv -Path "processes.csv" # 输出到 CSV
120
123
$data = Import-Csv -Path "data.csv" # 从 CSV 导入
121
124
```
122
125
123
- ## 系统管理
126
+ 系统管理
127
+ ---
128
+
129
+ ### 获取信息
124
130
125
- ``` powershell
131
+ ``` PowerShell
126
132
# 获取 BIOS 信息
127
133
Get-CimInstance -ClassName Win32_BIOS
128
134
# 获取本地连接的物理磁盘设备信息
@@ -133,7 +139,11 @@ Get-CimInstance -ClassName Win32_PhysicalMemory
133
139
Get-CimInstance -ClassName Win32_NetworkAdapter
134
140
# 获取安装的图形/显卡(GPU)信息
135
141
Get-CimInstance -ClassName Win32_VideoController
142
+ ```
143
+
144
+ ### 命名空间 & 类
136
145
146
+ ``` PowerShell
137
147
# 列出所有类名
138
148
Get-CimClass | Select-Object -ExpandProperty CimClassName
139
149
# 探索 root\cimv2 命名空间中的各种 WMI 类
@@ -144,7 +154,7 @@ Get-CimInstance -Namespace root -ClassName __NAMESPACE
144
154
145
155
### 网络管理
146
156
147
- ``` powershell
157
+ ``` PowerShell
148
158
# 测试与远程主机的网络连接
149
159
Test-Connection -ComputerName google.com
150
160
@@ -164,7 +174,7 @@ Test-NetConnection google.com -Port 80
164
174
165
175
### 用户和组管理
166
176
167
- ``` powershell
177
+ ``` PowerShell
168
178
# 获取本地用户账户信息
169
179
Get-LocalUser
170
180
@@ -183,7 +193,7 @@ Add-LocalGroupMember -Group Administrators -Member UserToAdd
183
193
184
194
### 安全性和权限
185
195
186
- ``` powershell
196
+ ``` PowerShell
187
197
# 获取文件/目录的访问控制列表
188
198
Get-Acl C:\Path\To\File.txt
189
199
@@ -193,7 +203,7 @@ Set-Acl -Path C:\Path\To\File.txt -AclObject $aclObject
193
203
194
204
### 注册表管理
195
205
196
- ``` powershell
206
+ ``` PowerShell
197
207
# 获取注册表键值
198
208
Get-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" | Select DisplayName, DisplayVersion
199
209
@@ -212,11 +222,11 @@ Test-Path "HKLM:\Software\MyApp"
212
222
213
223
## 脚本
214
224
215
- ### 变量
225
+ ### 变量
216
226
217
227
初始化变量,指定或不指定类型:
218
228
219
- ``` powershell
229
+ ``` PowerShell
220
230
$var = 0
221
231
[int] $var = 'Trevor' # (抛出异常)
222
232
[string] $var = 'Trevor' # (不会抛出异常)
@@ -234,7 +244,7 @@ $dict = @{k1 = 'test'; k2 = 'best'}
234
244
235
245
变量命令
236
246
237
- ``` Powershell
247
+ ``` PowerShell
238
248
New-Variable -Name FirstName -Value Trevor
239
249
New-Variable FirstName -Value Trevor -Option <ReadOnly/Constant>
240
250
@@ -251,7 +261,7 @@ Remove-Variable -Name firstname -Force
251
261
252
262
### 运算符
253
263
254
- ``` powershell
264
+ ``` PowerShell
255
265
# 运算符
256
266
# (a <op> b)
257
267
@@ -284,7 +294,7 @@ $regex.Matches('this is test').Value
284
294
285
295
#### 输入输出操作
286
296
287
- ``` powershell
297
+ ``` PowerShell
288
298
"This displays a string"
289
299
290
300
Write-Host "color" -ForegroundColor Red
@@ -298,12 +308,12 @@ Clear-Host
298
308
299
309
#### 流控制
300
310
301
- ``` powershell
311
+ ``` PowerShell
302
312
IF(<#Condition#>){
303
313
<#Commands#>}ELSEIF(){}ELSE{}
304
314
305
315
Switch($var){
306
- "val1"{<#Commands#>; break}
316
+ "val1"{<#Commands#>; break}
307
317
"val2"{<#Commands#>; break}
308
318
}
309
319
@@ -321,23 +331,23 @@ Do{}While()
321
331
322
332
#### 示例 1
323
333
324
- ``` powershell
334
+ ``` PowerShell
325
335
function funcname{
326
336
327
337
[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
333
343
return "value"
334
344
}
335
345
$var = funcname -user pcb
336
346
```
337
347
338
348
#### 示例 2
339
349
340
- ``` powershell
350
+ ``` PowerShell
341
351
function Get-EvenNumbers {
342
352
[CmdletBinding()]
343
353
param (
@@ -358,7 +368,7 @@ function Get-EvenNumbers {
358
368
359
369
#### 模块
360
370
361
- ``` powershell
371
+ ``` PowerShell
362
372
# PowerShell 在路径中查找模块
363
373
$env:PSModulePath
364
374
@@ -381,11 +391,9 @@ New-Module -Name trevor -ScriptBlock {
381
391
382
392
### 注意
383
393
384
-
385
394
- 在大多数语言中,转义字符是反斜杠 ** \\ ** ,而在 PowerShell 中是反引号 ** `**
386
395
387
-
388
396
## 参考
389
397
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)_
391
399
- [ cheatsheets] ( https://cheatsheets.zip/powershell )
0 commit comments