Cmdlet 网络与系统管理

PowerShell 网络 + 系统管理 Cmdlet 完全实战表(2025 最新版)

这些命令在 Windows、Linux、macOS 全平台通用(PowerShell 7+),运维、安全、开发必备!

任务推荐 Cmdlet + 最常用参数一键实战命令(直接复制就能用)
查看本机所有 IPGet-NetIPAddressGet-NetIPAddress -AddressFamily IPv4 | Select InterfaceAlias,IPAddress,PrefixLength
查看路由表Get-NetRouteGet-NetRoute -AddressFamily IPv4 | Sort DestinationPrefix
查看网卡详细信息Get-NetAdapterGet-NetAdapter | Where Status -eq Up | Select Name,Status,LinkSpeed
Ping(更好用的)Test-ConnectionTest-Connection 8.8.8.8 -Count 4 -Delay 1
连续 Ping + 实时显示Test-Connection -RepeatTest-Connection 114.114.114.114 -Repeat
Tracert(路径追踪)Test-NetConnection -TraceRouteTest-NetConnection baidu.com -TraceRoute
端口扫描 / 查端口占用Test-NetConnectionTest-NetConnection 192.168.1.1 -Port 3389 # 是否能连通
Get-NetTCPConnection -LocalPort 80
查看 DNS 缓存Get-DnsClientCacheGet-DnsClientCache | Where Entry -like "*github*"
刷新 DNSClear-DnsClientCacheClear-DnsClientCache; ipconfig /flushdns
查看当前所有网络连接Get-NetTCPConnectionGet-NetTCPConnection | Where State -eq Established | Select Local*,Remote*,OwningProcess | Get-Process -Id {$_.OwningProcess}
查看系统信息(最全)Get-CimInstance Win32_OperatingSystemGet-CimInstance Win32_OperatingSystem | Select Caption,Version,OSArchitecture,LastBootUpTime,TotalVisibleMemorySize
查看硬件信息Get-CimInstance Win32_ComputerSystemGet-CimInstance Win32_ComputerSystem | Select Manufacturer,Model,TotalPhysicalMemory
开机时间 & 运行时长(Get-Date) - (gcim Win32_OperatingSystem).LastBootUpTime"已运行:$((Get-Date) - (gcim Win32_OperatingSystem).LastBootUpTime)"
重启 / 关机Restart-Computer, Stop-ComputerRestart-Computer -Force
Stop-Computer -Force
远程执行命令(神器)Invoke-Command -ComputerNameInvoke-Command -ComputerName PC01,PC02 -ScriptBlock { hostname; Get-Date }
批量远程重启 100 台电脑Restart-Computer -ComputerName (Get-Content pcs.txt) -ForceGet-Content C:\pcs.txt | Restart-Computer -Force -WhatIf
查看事件日志(最新错误)Get-WinEvent(推荐)或 Get-EventLogGet-WinEvent -LogName System -MaxEvents 20 | Where LevelDisplayName -eq Error
查看 Windows 激活状态Get-CimInstance SoftwareLicensingProductGet-CimInstance SoftwareLicensingProduct | Where "Name like '*Windows*'" | Select Name,LicenseStatus
一键打开远程桌面Set-ItemPropertySet-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 等)”?

随时说,我给你安排更硬核的!

文章已创建 2965

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部