分享powershell設定網卡,ip,網關,dns的命令


-------------------------------------------------【系統需求】-------------------------------------------------

不支持win7,win2008r2

支持win8及以上

支持win2012及以上。

-------------------------------------------------【獲取網卡】-------------------------------------------------

獲取所有網卡:
Get-NetAdapter
 
 
獲取單個網卡:
Get-NetAdapter -Name "Ethernet0" #或以太網,
或:
Get-NetAdapter -InterfaceIndex 8
================================================================================================================
獲取網卡物理信息
Get-NetAdapterHardwareInfo

-------------------------------------------------【獲取ip,網關等】-------------------------------------------------

Get-NetIPConfiguration -InterfaceAlias Ethernet0 -Detailed#或以太網,
 
 
#獲取ip
(Get-NetAdapter -Name ethernet0 | Get-NetIPAddress).IPv4Address

-------------------------------------------------【重啟網卡】-------------------------------------------------

Restart-NetAdapter -InterfaceAlias Ethernet0 #或以太網
 

-------------------------------------------------【設定ip,網關】-------------------------------------------------

在遠程win2019上,更改ip。更改成功后會卡死,需要重新用新ip連接win2019。
(Get-NetAdapter)[-1] | New-NetIPAddress -AddressFamily IPv4 -IPAddress 192.168.11.220 -PrefixLength 8 -DefaultGateway 192.168.11.1
 
 
#更改ip
(Get-NetAdapter)[-1] | Set-NetIPAddress  -IPAddress 192.168.11.90

#設定成從dhcp獲取
(Get-NetAdapter)[-1] | Set-NetIPInterface -Dhcp Enabled
================================================================================================================
禁用ipv6
(Get-NetAdapter)[-1]| Disable-NetAdapterBinding -ComponentID ms_tcpip6

-------------------------------------------------【設定dns】-------------------------------------------------

(Get-NetAdapter)[-1] | Set-DNSClientServerAddress -ServerAddresses 192.168.11.1,114.114.114.114
 
 
#自動獲取dns
(Get-NetAdapter)[-1] |Set-DnsClientServerAddress -ResetServerAddresses

-------------------------------------------------【機子改名】-------------------------------------------------

Rename-Computer -ComputerName . -NewName 'ps傳教士2020' #這條命令不支持中文機子名,垃圾。
(Get-WmiObject win32_computersystem).rename('ps傳教士2021') #good

-------------------------------------------------【設定路由】-------------------------------------------------

添加:
New-NetRoute -DestinationPrefix "0.0.0.0/0" -NextHop "192.168.12.2" -InterfaceIndex 8
 
刪除:
Set-NetIPInterface -InterfaceAlias Ethernet0| Remove-NetRoute -Confirm:$false


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM