跟着AI Agent学powershell

87 阅读2分钟

跟着AI Agent学powershell

原文地址: pldz1.com/article/2fa…

视频制作工具: github.com/pldz1/html-…

更多参考: pldz1.com


跟着AI Agent学powershell

点击封面前往 B 站观看


之前,让codex配置了Windows的powershell版本到7之后,这里分享一些实用的ps的用法给大家

前排提示, powershell在windows上和 Windows terminal搭配起来 复用效果更好.

下面的内容包含了 AI的扩展,但是推荐大家阅读的已经打了 ⭐ 号标记


希望大家阅读完重点内容能回答这5个问题 哈哈哈

  1. 共享配置:$PROFILE 里用什么语法加载 shared-profile.ps1?

  2. 切换终端:在 PowerShell 里输入什么进入 CMD?

  3. 特有配置:关闭 PSReadLine 命令预测用什么命令?

  4. Linux 类似命令:PowerShell 7 里查看当前路径,可以直接输入什么?

  5. 特有指令:查询本机 8080 端口用什么 PowerShell 命令?


在这里插入图片描述

⭐ 1. 共享配置

让 Windows PowerShell 5.1 和 PowerShell 7 共用同一套配置。

$PROFILE

PowerShell 启动时会自动执行 $PROFILE

可以查看当前 Profile 路径:

$PROFILE

不同 PowerShell 版本的 $PROFILE 路径可能不同,因此不建议把所有配置分别复制到每个 Profile。

更适合的方式是:

PS5 Profile ──┐
              ├── shared-profile.ps1
PS7 Profile ──┘

Profile 内容

在 PowerShell 5.1 和 PowerShell 7 的 $PROFILE 中都写:

. "$HOME\Documents\PowerShell\shared-profile.ps1"

. 是 PowerShell 的 dot-sourcing。

作用是:

读取 shared-profile.ps1
并把其中定义的 function / alias / 设置
加载到当前 PowerShell 会话

这样以后只需要修改:

shared-profile.ps1

PS5 和 PS7 都会使用新的配置。


⭐ 2. 在不同终端 / Shell 之间切换

demo-gif

Windows Terminal 只是终端窗口。

里面可以运行不同 Shell:

cmd
Windows PowerShell 5.1
PowerShell 7
Git Bash
WSL2 Ubuntu

很多时候不需要重新开窗口,直接在当前 Shell 中启动另一个 Shell 即可。

PowerShell → Git Bash

自定义一个函数:

function git-bash {
    & "D:\Git\bin\bash.exe" --login -i
}

因此可以直接:

git-bash

进入 Git Bash。

Shell 切换关系

可以把它理解成:

Windows Terminal
       │
       ├── cmd
       │
       ├── powershell
       │      └── Windows PowerShell 5.1
       │
       ├── pwsh
       │      └── PowerShell 7
       │
       ├── git-bash
       │      └── Git Bash
       │
       └── wsl
              └── WSL2 Ubuntu

常用:

当前 Shell              输入

PowerShell → CMD         cmd
CMD → PS5                powershell
CMD → PS7                pwsh
PS5 → PS7                pwsh
PS7 → PS5                powershell
PS → Git Bash            git-bash
PS / CMD → WSL           wsl
PS / CMD → Ubuntu        wsl -d Ubuntu
返回上一层                exit

3. PowerShell 特有配置与历史清理

⭐ 3.1 PS 7 关闭命令预测

PowerShell 的 PSReadLine 默认可能显示命令预测。

auto-i

例如输入:

git

后面可能出现灰色预测内容。

如果不需要,而且觉得妨碍输入,可以关闭:

Set-PSReadLineOption -PredictionSource None

已经放入:

shared-profile.ps1

所以每次启动 PS5 / PS7 都会自动关闭。


3.2 PowerShell 的历史记录和 CMD 不完全一样

PowerShell 的历史至少要区分两个部分:

PowerShell Session History
+
PSReadLine Persistent History

因此:

Clear-History

并不一定等于:

彻底删除以前输入过的所有命令

3.3 清理当前 PowerShell History

Clear-History

查看:

Get-History

3.4 PSReadLine 的磁盘历史

PSReadLine 会把历史保存到文件。

查看这个文件在哪里:

(Get-PSReadLineOption).HistorySavePath

删除:

(Get-PSReadLineOption).HistorySavePath |
    Remove-Item

为了避免文件不存在时报错:

(Get-PSReadLineOption).HistorySavePath |
    Remove-Item -ErrorAction SilentlyContinue

3.5 清屏

Clear-Host

也可以直接:

cls

或者:

clear

3.6 一次全部清理

定义:

function Clear-All {
    Clear-Host
    Clear-History
    (Get-PSReadLineOption).HistorySavePath |
        Remove-Item -ErrorAction SilentlyContinue
}

再定义:

Set-Alias cla Clear-All

以后:

cla

相当于:

清屏
+
清当前 PowerShell History
+
删除 PSReadLine 持久化历史

注意这里主要清理的是:

命令历史

严格来说并不是传统意义上的系统 cache。


4. ⭐ 和 Linux / Bash 类似的常用命令

PowerShell 可以直接使用很多和 Bash 类似的名字。

当前路径

Bash:

pwd

PowerShell:

pwd

本体:

Get-Location

查看目录

ls

PowerShell:

Get-ChildItem

自定义:

ll

也是:

Get-ChildItem

查看隐藏文件:

Get-ChildItem -Force

递归:

Get-ChildItem -Recurse

切换目录

cd D:\work

本体:

Set-Location D:\work

查看文件

cat file.txt

本体:

Get-Content file.txt

前 20 行:

Get-Content file.txt -Head 20

后 20 行:

Get-Content file.txt -Tail 20

实时查看日志:

Get-Content app.log -Tail 50 -Wait

类似:

tail -f app.log

复制

cp file1 file2

本体:

Copy-Item

移动 / 重命名

mv old.txt new.txt

本体:

Move-Item

删除

rm file.txt

本体:

Remove-Item

递归删除:

Remove-Item folder -Recurse

创建目录

mkdir test

也可以:

New-Item -ItemType Directory test

进程 ps

可以:

ps

PowerShell 本体:

Get-Process

例如:

ps

直接查看当前进程。

查询 Python:

Get-Process python

Kill

PowerShell:

Stop-Process -Id 1234

或者:

Stop-Process -Name python

强制:

Stop-Process -Id 1234 -Force

grep

PowerShell:

Select-String "error" file.log

例如:

Select-String "error" *.log

递归:

Get-ChildItem -Recurse -File |
    Select-String "error"

find

类似:

find . -name "*.txt"

PowerShell:

Get-ChildItem -Recurse -Filter "*.txt"

which

Bash:

which python

PowerShell 更推荐:

Get-Command python

只看路径:

(Get-Command python).Source

如果存在多个 Python:

Get-Command python -All

Windows 自带命令也可以:

where.exe python

查 Git / Python / Bash 在哪里

Get-Command python
Get-Command pip
Get-Command git
Get-Command bash
Get-Command java

例如:

(Get-Command python).Source

可以直接得到 Python executable 路径。


环境变量

类似 Bash:

echo $PATH

PowerShell:

$env:PATH

查看全部:

Get-ChildItem Env:

常见对应

Linux / Bash        PowerShell

pwd                 pwd / Get-Location
ls                  ls / Get-ChildItem
cat                 cat / Get-Content
cd                  cd / Set-Location
cp                  cp / Copy-Item
mv                  mv / Move-Item
rm                  rm / Remove-Item
mkdir               mkdir / New-Item

ps                  ps / Get-Process
kill                Stop-Process

grep                Select-String
find                Get-ChildItem -Recurse
which               Get-Command
which -a            Get-Command -All

head                Get-Content -Head
tail                Get-Content -Tail
tail -f             Get-Content -Tail -Wait

sort                Sort-Object
uniq                Sort-Object -Unique
wc                  Measure-Object

ping                Test-Connection
dig                 Resolve-DnsName
traceroute          Test-NetConnection -TraceRoute

5. PowerShell 常用特有命令

这部分是 PowerShell 比单纯模仿 Linux 命令更有价值的地方。


5.1 查询程序在哪里

Get-Command python

例如:

Get-Command python
Get-Command pip
Get-Command git
Get-Command pwsh
Get-Command powershell
Get-Command bash

只取 executable 路径:

(Get-Command python).Source

查看所有匹配项:

Get-Command python -All

5.2 查询程序类型

例如:

Get-Command python

可能显示:

CommandType    Name          Source
-----------    ----          ------
Application    python.exe    ...

而:

Get-Command ll

可能显示:

Alias
Get-Command git-bash

会显示:

Function

因此 Get-Command 不只是 which,还能判断它究竟是:

Application
Cmdlet
Function
Alias
Script

5.3 查询进程

Get-Process

查 Python:

Get-Process python

查 Chrome:

Get-Process *chrome*

按照 CPU 时间排序:

Get-Process |
    Sort-Object CPU -Descending |
    Select-Object -First 10 Name, Id, CPU

按照内存排序:

Get-Process |
    Sort-Object WorkingSet -Descending |
    Select-Object -First 10 ProcessName,
        @{N='MemoryMB';E={[math]::Round($_.WorkingSet / 1MB, 1)}}

5.4 查询线程

指定 PID:

(Get-Process -Id 1234).Threads

统计每个进程线程数量:

Get-Process |
    Select-Object Id, ProcessName,
        @{N='Threads';E={$_.Threads.Count}} |
    Sort-Object Threads -Descending

5.5 查询 TCP 连接

Get-NetTCPConnection

相当于 Linux 常用:

ss

或者:

netstat

5.6 查询正在监听的端口

Get-NetTCPConnection -State Listen

例如查 8080:

Get-NetTCPConnection -LocalPort 8080

重要字段:

LocalAddress
LocalPort
RemoteAddress
RemotePort
State
OwningProcess

5.7 查询哪个程序占用了端口

例如查 8080:

Get-NetTCPConnection -LocalPort 8080

假设:

OwningProcess = 1234

然后:

Get-Process -Id 1234

也可以直接:

Get-NetTCPConnection -LocalPort 8080 |
    ForEach-Object {
        Get-Process -Id $_.OwningProcess
    }

5.8 UDP

Get-NetUDPEndpoint

5.9 测试端口能不能连接

例如:

Test-NetConnection github.com -Port 443

数据库:

Test-NetConnection localhost -Port 5432

Web 服务:

Test-NetConnection localhost -Port 8080

这是排查:

服务有没有开
端口有没有监听
防火墙有没有拦
网络能不能连通

时非常实用的命令。


5.10 Ping

Test-Connection google.com

例如:

Test-Connection google.com -Count 2

5.11 DNS

Resolve-DnsName github.com

5.12 路由

Test-NetConnection github.com -TraceRoute

5.13 Windows 服务

查看:

Get-Service

查某类服务:

Get-Service *docker*

启动:

Start-Service sshd

停止:

Stop-Service sshd

重启:

Restart-Service sshd

5.14 查询对象有哪些属性

PowerShell 很重要的命令:

Get-Member

例如:

Get-Process | Get-Member

可以查看 Get-Process 输出对象拥有哪些:

Property
Method
Member

如果不知道:

“这个结果还能查什么?”

就可以:

... | Get-Member

例如:

Get-NetTCPConnection | Get-Member

5.15 对结果进行筛选

例如只看 CPU 大于 10 的进程:

Get-Process |
    Where-Object CPU -gt 10

完整写法:

Get-Process |
    Where-Object { $_.CPU -gt 10 }

5.16 选择字段

Get-Process |
    Select-Object Id, ProcessName, CPU

5.17 排序

Get-Process |
    Sort-Object CPU -Descending

5.18 只取前几个

Get-Process |
    Select-Object -First 10

组合:

Get-Process |
    Sort-Object CPU -Descending |
    Select-Object -First 10 Id, ProcessName, CPU