Windows PowerShell 命令笔记

PowerShell Windows 命令行 MeetYou
创建于 2026-05-09
目录

Windows PowerShell 命令笔记

Summary

PowerShell 是 Windows 上更现代的命令行环境。它适合做本地开发、脚本、JSON 处理、端口检查、进程管理、调用 HTTP 接口、启动 Python/Node 项目。对 MeetYou 来说,它主要用于:本地拉代码、启动 UI/desktop agent、执行构建脚本、SSH 到服务器、检查远程 `/health`。

相关:00-MOC-命令行与部署知识库总览02-Windows-CMD-命令笔记05-Git-SSH-GitHub-命令笔记06-MeetYou-Core-部署运维手册

1. PowerShell 和 CMD 的核心区别

PowerShell CMD
管道传递 对象 字符串
命令风格 Verb-Noun,如 Get-ChildItem 传统命令,如 dir
结构化数据 很强,适合 JSON/对象
脚本扩展名 .ps1 .bat / .cmd
推荐场景 现代开发、自动化、系统管理 老工具、批处理兼容

PowerShell 可以使用很多 Linux/CMD 风格别名,比如 lscatdir,但本质上它们可能映射到 PowerShell cmdlet,不一定和 Linux 行为完全一致。

2. 基础导航与目录

pwd                         # 当前目录;等价 Get-Location
Get-Location                # 当前目录
cd C:\Projects\MeetYou      # 切换目录;等价 Set-Location
cd ..                       # 上一级
cd ~                        # 用户主目录
ls                          # 列目录;等价 Get-ChildItem
dir                         # 也可列目录
Get-ChildItem -Force        # 显示隐藏文件
Get-ChildItem -Recurse      # 递归列出

Tip

路径里有空格必须加引号:
cd "C:\Users\Your Name\Projects"

3. 文件与目录操作

New-Item -ItemType Directory logs       # 新建目录
New-Item app.log                        # 新建文件
Copy-Item a.txt b.txt                   # 复制文件
Copy-Item .\dist .\backup -Recurse      # 复制目录
Move-Item a.txt .\archive\              # 移动/重命名
Remove-Item a.txt                       # 删除文件
Remove-Item .\dist -Recurse -Force      # 删除目录,谨慎使用
Rename-Item old.txt new.txt             # 重命名

安全删除习惯:

Get-ChildItem .\dist                    # 先看
Remove-Item .\dist -Recurse -Force      # 再删

4. 查看、搜索、写入文本

Get-Content .\README.md                 # 查看文件
Get-Content .\logs\app.log -Tail 50     # 看最后 50 行
Get-Content .\logs\app.log -Wait        # 类似 Linux tail -f
Select-String -Path .\*.md -Pattern "Docker"      # 搜索文本
Select-String -Path .\**\*.py -Pattern "TODO"     # 递归搜索可能需 PS 7+
"hello" | Out-File .\hello.txt -Encoding utf8      # 写入文件
"more" | Add-Content .\hello.txt                  # 追加

更推荐编辑配置文件时使用 VS Code:

code .
code .env
code user\config.json

5. 环境变量

5.1 当前窗口临时变量

$env:MEETYOU_ENV = "dev"
$env:MEETYOU_ENV

只在当前 PowerShell 窗口有效。关闭窗口后消失。

5.2 用户级持久变量

[Environment]::SetEnvironmentVariable("MEETYOU_ENV", "dev", "User")

重新打开终端后生效。

5.3 查看 PATH

$env:Path -split ";"

6. Python 与虚拟环境

python --version
py --version
python -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
pip install -r requirements.txt

如果激活虚拟环境时报执行策略错误:

Set-ExecutionPolicy -Scope CurrentUser RemoteSigned

Warning

不要随手用 `Unrestricted`。一般 `RemoteSigned` 足够。

7. Node / 前端项目常用命令

node -v
npm -v
npm install
npm run dev
npm run build
npm test

如果使用 pnpm:

corepack enable
pnpm install
pnpm dev
pnpm build

MeetYou UI 常见链路:

cd C:\Projects\MeetYou\meetyou-ui
npm install
npm run dev

8. 进程、端口与网络诊断

8.1 查端口占用

Get-NetTCPConnection -LocalPort 38951
Get-NetTCPConnection -LocalPort 8000

配合进程:

$pid = (Get-NetTCPConnection -LocalPort 38951).OwningProcess
Get-Process -Id $pid
Stop-Process -Id $pid -Force

CMD 风格也常用:

netstat -ano | findstr :38951
taskkill /PID 12345 /F

8.2 测连接

Test-NetConnection github.com -Port 443
Test-NetConnection core.maziteng.cn -Port 443

8.3 请求 HTTP 接口

Invoke-WebRequest https://core.maziteng.cn/health
Invoke-RestMethod https://core.maziteng.cn/health

带 Header:

$token = "替换成你的 token"
Invoke-RestMethod "https://core.maziteng.cn/health" -Headers @{ Authorization = "Bearer $token" }

Note

Windows PowerShell 里 `curl` 可能是 `Invoke-WebRequest` 的别名,行为和 Linux curl 不完全一样。需要原生 curl 时可用 `curl.exe`。

9. Git 常用命令

git status
git diff
git diff --name-only HEAD~1..HEAD
git add .
git commit -m "docs: update command notes"
git pull --ff-only
git push

判断这次更新是否影响远程 Core:

git diff --name-only 旧提交..新提交

如果输出里有这些目录,通常远程 Core 也要更新:

core/
gateway/
service_runtime/
adapters/
alembic/
deploy/systemd/
requirements-core.txt

10. SSH 到服务器

ssh ubuntu@服务器IP
ssh ubuntu@core.maziteng.cn

指定私钥:

ssh -i C:\Users\\.ssh\id_ed25519 ubuntu@服务器IP

拷贝文件到服务器:

scp .\local.txt ubuntu@服务器IP:/tmp/local.txt

11. PowerShell 脚本

脚本文件:deploy.ps1

$ErrorActionPreference = "Stop"

Write-Host "Checking git status..."
git status

Write-Host "Running tests..."
python -m pytest

Write-Host "Done."

运行:

.\deploy.ps1

12. MeetYou 本地常用片段

12.1 检查本地 bridge 端口

Get-NetTCPConnection -LocalPort 38951 -ErrorAction SilentlyContinue

12.2 调本地 bridge 状态

Invoke-RestMethod http://127.0.0.1:38951/desktop/bridge/status

12.3 检查远程 Core 健康

Invoke-RestMethod https://core.maziteng.cn/health

若需要 Bearer Token:

$token = "不要把真实 token 写进普通笔记"
Invoke-RestMethod https://core.maziteng.cn/health -Headers @{ Authorization = "Bearer $token" }

13. 常见坑

问题 原因 处理
.ps1 cannot be loaded 执行策略限制 Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
curl 行为和教程不同 PowerShell 中 curl 是别名 curl.exeInvoke-RestMethod
路径带空格报错 没加引号 "C:\Users\Your Name"
改了环境变量没生效 当前窗口未刷新 重新打开终端
端口被占用 上次进程没关 Get-NetTCPConnection + Stop-Process

14. 自测题

  1. 如何在 PowerShell 中查看 38951 端口被哪个进程占用?
  2. curl 在 PowerShell 里为什么可能和 Linux 不同?
  3. 当前窗口临时环境变量和用户级持久环境变量有什么区别?
  4. 本地启动 MeetYou UI 前,为什么要先确认 desktop_agent 或 bridge 端口?

我的新增记录


反向链接:00-MOC-命令行与部署知识库总览07-常见排错与小点整合