Skip to content

Commit

Permalink
doc: update docs/powershell.md #845
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 29, 2024
1 parent 2c6244b commit 3662bb7
Showing 1 changed file with 56 additions and 48 deletions.
104 changes: 56 additions & 48 deletions docs/powershell.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
## 基本命令
PowerShell 备忘清单
===

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

常用操作
---

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

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

| 动词 | 描述 |
| ------- | ------------------------ |
Expand All @@ -22,86 +26,85 @@

列出可用模块

```powershell
```PowerShell
Get-Module --ListAvailable
```

列出可用的 cmdlet 和函数

```powershell
```PowerShell
Get-Command -Module ActiveDirectory
```

列出别名及其对应的 cmdlet 名称

```PowerShell
Get-Alias | Select-Object Name, Definition
```

获取帮助

```powershell
```PowerShell
Get-Help <cmd>
Get-Help <cmd> -Examples
Get-Help -Name Get-Process -Parameter Id
```

列出别名及其对应的 cmdlet 名称


```powershell
Get-Alias | Select-Object Name, Definition
```

**Get-Member:** 显示对象的属性和方法

```powershell
```PowerShell
Get-Process | Get-Member
```

### 对象操作

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

```powershell
```PowerShell
Get-Process | Select-Object Name, CPU
```

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

```powershell
```PowerShell
Get-Service | Where-Object { $PSItem.Status -eq 'Running' }
#OR
Get-Service | ? { $_.Status -eq 'Running' }
```

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

```powershell
```PowerShell
Get-Process | Measure-Object -Property WorkingSet -Sum
```

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

```powershell
```PowerShell
Get-ChildItem | ForEach-Object { Rename-Item $_ -NewName "Prefix_$_" }
```

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

```powershell
```PowerShell
Get-ChildItem | Sort-Object Length -Descending
```

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

```powershell
```PowerShell
Get-Service | Format-Table -AutoSize # ft alias
```

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

```powershell
```PowerShell
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 'dir'
Copy-Item <src> -destination <dest>
Expand All @@ -120,9 +123,12 @@ Get-Process | Export-Csv -Path "processes.csv" # 输出到 CSV
$data = Import-Csv -Path "data.csv" # 从 CSV 导入
```

## 系统管理
系统管理
---

### 获取信息

```powershell
```PowerShell
# 获取 BIOS 信息
Get-CimInstance -ClassName Win32_BIOS
# 获取本地连接的物理磁盘设备信息
Expand All @@ -133,7 +139,11 @@ Get-CimInstance -ClassName Win32_PhysicalMemory
Get-CimInstance -ClassName Win32_NetworkAdapter
# 获取安装的图形/显卡(GPU)信息
Get-CimInstance -ClassName Win32_VideoController
```

### 命名空间 & 类

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

### 网络管理

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

### 用户和组管理

```powershell
```PowerShell
# 获取本地用户账户信息
Get-LocalUser
Expand All @@ -183,7 +193,7 @@ Add-LocalGroupMember -Group Administrators -Member UserToAdd

### 安全性和权限

```powershell
```PowerShell
# 获取文件/目录的访问控制列表
Get-Acl C:\Path\To\File.txt
Expand All @@ -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
Expand All @@ -212,11 +222,11 @@ Test-Path "HKLM:\Software\MyApp"

## 脚本

### 变量
### 变量

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

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

变量命令

```Powershell
```PowerShell
New-Variable -Name FirstName -Value Trevor
New-Variable FirstName -Value Trevor -Option <ReadOnly/Constant>
Expand All @@ -251,7 +261,7 @@ Remove-Variable -Name firstname -Force

### 运算符

```powershell
```PowerShell
# 运算符
# (a <op> b)
Expand Down Expand Up @@ -284,7 +294,7 @@ $regex.Matches('this is test').Value

#### 输入输出操作

```powershell
```PowerShell
"This displays a string"
Write-Host "color" -ForegroundColor Red
Expand All @@ -298,12 +308,12 @@ Clear-Host

#### 流控制

```powershell
```PowerShell
IF(<#Condition#>){
<#Commands#>}ELSEIF(){}ELSE{}
Switch($var){
"val1"{<#Commands#>; break}
"val1"{<#Commands#>; break}
"val2"{<#Commands#>; break}
}
Expand All @@ -321,23 +331,23 @@ Do{}While()

#### 示例 1

```powershell
```PowerShell
function funcname{
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[String]$user
)
Write-Host "welcome " $user
param(
[Parameter(Mandatory)]
[String]$user
)
Write-Host "welcome " $user
return "value"
}
$var = funcname -user pcb
```

#### 示例 2

```powershell
```PowerShell
function Get-EvenNumbers {
[CmdletBinding()]
param (
Expand All @@ -358,7 +368,7 @@ function Get-EvenNumbers {

#### 模块

```powershell
```PowerShell
# PowerShell 在路径中查找模块
$env:PSModulePath
Expand All @@ -381,11 +391,9 @@ New-Module -Name trevor -ScriptBlock {

### 注意


- 在大多数语言中,转义字符是反斜杠 **\\**,而在 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)

0 comments on commit 3662bb7

Please sign in to comment.