PowerShell 网络 + 系统管理 Cmdlet 完全实战表(2025 最新版)
这些命令在 Windows、Linux、macOS 全平台通用(PowerShell 7+),运维、安全、开发必备!
| 任务 | 推荐 Cmdlet + 最常用参数 | 一键实战命令(直接复制就能用) |
|---|---|---|
| 查看本机所有 IP | Get-NetIPAddress | Get-NetIPAddress -AddressFamily IPv4 | Select InterfaceAlias,IPAddress,PrefixLength |
| 查看路由表 | Get-NetRoute | Get-NetRoute -AddressFamily IPv4 | Sort DestinationPrefix |
| 查看网卡详细信息 | Get-NetAdapter | Get-NetAdapter | Where Status -eq Up | Select Name,Status,LinkSpeed |
| Ping(更好用的) | Test-Connection | Test-Connection 8.8.8.8 -Count 4 -Delay 1 |
| 连续 Ping + 实时显示 | Test-Connection -Repeat | Test-Connection 114.114.114.114 -Repeat |
| Tracert(路径追踪) | Test-NetConnection -TraceRoute | Test-NetConnection baidu.com -TraceRoute |
| 端口扫描 / 查端口占用 | Test-NetConnection | Test-NetConnection 192.168.1.1 -Port 3389 # 是否能连通Get-NetTCPConnection -LocalPort 80 |
| 查看 DNS 缓存 | Get-DnsClientCache | Get-DnsClientCache | Where Entry -like "*github*" |
| 刷新 DNS | Clear-DnsClientCache | Clear-DnsClientCache; ipconfig /flushdns |
| 查看当前所有网络连接 | Get-NetTCPConnection | Get-NetTCPConnection | Where State -eq Established | Select Local*,Remote*,OwningProcess | Get-Process -Id {$_.OwningProcess} |
| 查看系统信息(最全) | Get-CimInstance Win32_OperatingSystem | Get-CimInstance Win32_OperatingSystem | Select Caption,Version,OSArchitecture,LastBootUpTime,TotalVisibleMemorySize |
| 查看硬件信息 | Get-CimInstance Win32_ComputerSystem | Get-CimInstance Win32_ComputerSystem | Select Manufacturer,Model,TotalPhysicalMemory |
| 开机时间 & 运行时长 | (Get-Date) - (gcim Win32_OperatingSystem).LastBootUpTime | "已运行:$((Get-Date) - (gcim Win32_OperatingSystem).LastBootUpTime)" |
| 重启 / 关机 | Restart-Computer, Stop-Computer | Restart-Computer -ForceStop-Computer -Force |
| 远程执行命令(神器) | Invoke-Command -ComputerName | Invoke-Command -ComputerName PC01,PC02 -ScriptBlock { hostname; Get-Date } |
| 批量远程重启 100 台电脑 | Restart-Computer -ComputerName (Get-Content pcs.txt) -Force | Get-Content C:\pcs.txt | Restart-Computer -Force -WhatIf |
| 查看事件日志(最新错误) | Get-WinEvent(推荐)或 Get-EventLog | Get-WinEvent -LogName System -MaxEvents 20 | Where LevelDisplayName -eq Error |
| 查看 Windows 激活状态 | Get-CimInstance SoftwareLicensingProduct | Get-CimInstance SoftwareLicensingProduct | Where "Name like '*Windows*'" | Select Name,LicenseStatus |
| 一键打开远程桌面 | Set-ItemProperty | Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Terminal Server" -Name "fDenyTSConnections" -Value 0; netsh advfirewall firewall add rule name="RDP" dir=in action=allow protocol=TCP localport=3389 |
10 个运维/安全/办公必备“一键脚本”
# 1. 一键诊断网络连通性(比 ipconfig 强 100 倍)
Test-Connection 8.8.8.8,1.1.1.1,114.114.114.114 -Count 2 | Select Destination,ResponseTime,Status
# 2. 找出本机所有开放的端口(安全审计必备)
Get-NetTCPConnection | Where State -eq Listen | Select LocalAddress,LocalPort,OwningProcess | Sort LocalPort
# 3. 批量检测 1000 台电脑是否在线(10 秒出结果)
$ips = "192.168.1.1..192.168.1.254"
$ips | ForEach-Object -Parallel { if (Test-Connection $_ -Count 1 -Quiet) { "$_ 在线" } } -ThrottleLimit 100
# 4. 一键生成电脑健康报告(导出 Excel)
Import-Module ImportExcel -ErrorAction SilentlyContinue
[pscustomobject]@{
电脑名 = $env:COMPUTERNAME
系统 = (Get-CimInstance Win32_OperatingSystem).Caption
内存GB = [math]::Round((Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory/1GB,2)
已运行 = (Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
IP地址 = (Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias *以太网*).IPAddress
} | Export-Excel "$env:USERPROFILE\Desktop\$(hostname)_健康报告.xlsx" -AutoSize -TableName 报告
# 5. 自动切换 DNS 到 114(解锁神器)
Get-NetAdapter | Where Status -eq Up | Set-DnsClientServerAddress -ServerAddresses 114.114.114.114,8.8.8.8
# 6. 查看最近 10 次关机/重启事件
Get-WinEvent -LogName System -MaxEvents 20 | Where Id -in 1074,6006,6008 | Select TimeCreated,Message
# 7. 一键关闭所有远程功能(安全加固)
Disable-WindowsOptionalFeature -Online -FeatureName MicrosoftWindowsPowerShellV2, TelnetClient, SMB1Protocol
掌握这些,你在任何环境下都能 30 秒定位网络问题、1 分钟完成系统巡检、5 分钟批量管理上千台电脑!
下一步你想要:
- “内网渗透测试常用命令集”?
- “写一个图形化网络监控仪表盘”?
- 还是“Linux 下的系统管理对比(systemctl、ss、ip 等)”?
随时说,我给你安排更硬核的!