Windows - PowerShell - PowerShell 常用命令

💡 快捷键 Win + XI 打开 PowerShell,Win + R 输入 powershell

🔌 基础操作

命令说明示例
Get-Help查看帮助Get-Help Get-Process
Get-Command查看命令Get-Command -Verb Get
Get-Member查看对象成员obj \| Get-Member
Get-Alias查看别名-

📁 文件操作

命令说明示例
Get-ChildItem / ls / dir列出文件ls -Recurse
Set-Location / cd切换目录cd C:\
New-Item创建New-Item -ItemType Directory folder
Remove-Item / rm删除Remove-Item -Recurse -Force
Copy-Item / cp复制Copy-Item src dest
Move-Item / mv移动Move-Item a.txt folder\
Get-Content / cat读取Get-Content a.txt
Set-Content写入Set-Content a.txt "hello"
Test-Path检查路径Test-Path a.txt

⚙️ 进程管理

命令说明示例
Get-Process查看进程Get-Process python
Stop-Process结束进程Stop-Process -Name notepad -Force
Start-Process启动进程Start-Process notepad
Wait-Process等待进程-

🔧 服务管理

命令说明示例
Get-Service查看服务Get-Service
Start-Service启动服务Start-Service Spooler
Stop-Service停止服务Stop-Service Spooler
Restart-Service重启服务Restart-Service Spooler
Set-Service修改服务-

🌐 网络操作

命令说明示例
Test-Connection / pingping 测试Test-Connection google.com
Resolve-DnsName / nslookupDNS 查询Resolve-DnsName google.com
Get-NetIPAddress查看 IP-
Get-NetAdapter网卡信息-
New-NetFirewallRule创建防火墙规则-
Test-NetConnection端口测试Test-NetConnection google.com -Port 443

💾 系统信息

命令说明
Get-ComputerInfo系统详细信息
Get-WmiObjectWMI 信息
Get-EventLog事件日志
Get-EventLog -LogName System系统日志
systeminfo兼容命令

📦 模块操作

# 查看已加载模块
Get-Module

# 查看可安装模块
Find-Module -Name *

# 安装模块
Install-Module -Name

# 导入模块
Import-Module

🔧 管道和筛选

# 筛选
Get-Process | Where-Object CPU -gt 10
Get-Process | Where-Object { $_.CPU -gt 10 -and $_.Name -eq 'chrome' }

# 排序
Get-Process | Sort-Object CPU -Descending

# 选择
Get-Process | Select-Object Name, CPU, Id

# 统计
(Get-Process).Count

# 格式化输出
Get-Process | Format-Table Name, CPU
Get-Process | Format-List *

📝 常用快捷变量

变量说明
$_当前管道对象
$PSVersionTablePS 版本
$env:VAR环境变量
$HOME用户目录
$PWD当前目录

🛠️ 常用环境变量

# 查看环境变量
$env:Path

# 添加到 PATH
$env:Path += ";C:\Tools\bin"

# 临时设置
$env:JAVA_HOME = "C:\Java\jdk17"

🔄 循环和条件

# ForEach
1..10 | ForEach-Object { $_ * 2 }

# ForEach-Object
Get-Process | ForEach-Object { $_.Name }

# 条件
if ($a -eq 1) { "one" }
switch ($status) { 1 { "OK" } 2 { "Fail" } }

# For
for ($i=0; $i -lt 10; $i++) { $i }

# While
while ($true) { break }

⚡ 常用快捷命令

# 快速打开文件位置
(Get-Item file.txt).DirectoryName

# 管理员权限
Start-Process powershell -Verb RunAs

# 定时任务
schtasks /create /sc minute /mo 1 /tn "MyTask" /tr "powershell -file script.ps1"

# 字符串操作
"hello" .ToUpper()
"hello" .Replace("l", "r")
"a,b,c" -split ","

📜 一行命令

# 杀掉所有 python 进程
Get-Process python -ErrorAction SilentlyContinue | Stop-Process

# 批量重命名
Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace 'old', 'new' }

# 查找大文件
Get-ChildItem -Recurse | Sort-Object Length -Descending | Select -First 10

# 端口占用
netstat -ano | Select-String ":8080"

文档信息

Search

    Table of Contents