从 CMD 迁移到 PowerShell 的常用命令对照表及实用代码片段。
| CMD | PowerShell | 说明 |
|---|
ping | Test-NetConnection | 网络连通性 |
ping -t | Test-Connection | 持续 Ping |
ipconfig | Get-NetIPAddress | IP 地址 |
ipconfig /flushdns | Clear-DnsClientCache | 清 DNS 缓存 |
tracert | Test-NetConnection -TraceRoute | 路由追踪 |
netstat -ano | Get-NetTCPConnection | TCP 连接 |
nslookup | Resolve-DnsName | DNS 查询 |
Test-NetConnection www.baidu.com
Resolve-DnsName www.baidu.com
Clear-DnsClientCache
Test-NetConnection www.baidu.com -Port 443
| CMD | PowerShell | 说明 |
|---|
dir | Get-ChildItem / ls | 列出文件 |
cd | Set-Location / cd | 切换目录 |
md | New-Item -ItemType Directory | 新建目录 |
rd /s /q | Remove-Item -Recurse -Force | 删除目录 |
copy | Copy-Item / cp | 复制 |
move | Move-Item | 移动 |
del | Remove-Item / rm | 删除 |
| CMD | PowerShell | 说明 |
|---|
tasklist | Get-Process | 进程列表 |
taskkill /PID | Stop-Process -Id | 杀进程 |
systeminfo | Get-ComputerInfo | 系统信息 |
hostname | hostname / $env:COMPUTERNAME | 主机名 |
whoami | whoami / $env:USERNAME | 当前用户 |
Get-Process chrome
Stop-Process -Name chrome -Force
| CMD | PowerShell | 说明 |
|---|
sc query | Get-Service | 查服务 |
sc start | Start-Service | 启动 |
sc stop | Stop-Service | 停止 |
sc config | Set-Service | 配置 |
Get-Service | Where-Object {$_.Status -eq 'Running'}
Restart-Service spooler
| CMD | PowerShell | 说明 |
|---|
net user | Get-LocalUser | 本地用户 |
net group | Get-LocalGroup | 本地组 |
icacls | Get-Acl / Set-Acl | 权限 |
Get-ChildItem * -Include .git -Force -Recurse | Remove-Item -Recurse -Force
$i = 0
Get-ChildItem -Path D:\pictures -Filter *.jpg |
ForEach-Object {
$extension = $_.Extension
$newName = 'pic_{0:d6}{1}' -f $i, $extension
$i++
Rename-Item -Path $_.FullName -NewName $newName
}
$startFolder = "D:\flutter"
$colItems = Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object
foreach ($i in $colItems) {
$subFolderItems = Get-ChildItem $i.FullName -recurse | Measure-Object -property length -sum
if ($subFolderItems.sum -gt 1GB) {
$FileSize = "{0:N2}" -f ($subFolderItems.sum / 1GB)
$Unit = 'GB'
} else {
$FileSize = "{0:N2}" -f ($subFolderItems.sum / 1MB)
$Unit = 'MB'
}
Write-Host "$($i.FullName) -- $FileSize $Unit" -ForegroundColor Green
}
param (
[String]$Type = "run"
)
$name = "server"
$curDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
Write-Host "当前路径 $curDir" -ForegroundColor Yellow
if ($Type -eq "run") {
Write-Host "运行 go run main.go" -ForegroundColor Cyan
} elseif ($Type -eq "build") {
Write-Host "编译 go build" -ForegroundColor Red
}
使用 . .\addParam.ps1 -Type 输入 - 会自动弹出 -Type 提示。
$command = "Get-Process | Sort-Object -Property CPU -Descending | Select-Object -First 5"
Invoke-Expression -Command $command
$command = "notepad.exe"
& $command
$commandString = "Get-ChildItem C:\TEST1"
$commandBlock = [scriptblock]::Create($commandString)
& $commandBlock
Get-Process -Id (Get-NetTCPConnection -LocalPort 5000).OwningProcess
Get-CimInstance Win32_Process |
Where-Object CommandLine -Match 'zfile' |
Remove-CimInstance
Stop-Process -Id 34328
New-Service -Name Redis `
-DisplayName Redis7 `
-BinaryPathName 'D:\programs\Redis-7.0.13-Windows-x64-with-Service\RedisService.exe' `
-StartupType Automatic
Invoke-WebRequest "http://p9.pstatp.com" -OutFile a.webp
Get-Command -Name npm
Start-Process (Get-Item (Get-Command -Name npm).Source).Directory
Start-Process -FilePath "powershell" -Verb RunAs
New-Item -ItemType File -Name a.txt
$host.Version
chcp
.\fileName.exe