Cmdlet Network and System Administration
<PowerShell can not only manage local files, processes, and services, but it also has powerful network functions and system-level operation capabilities. Whether it's viewing IP addresses, testing network connectivity, or getting system information and managing user accounts, PowerShell provides rich and unified Cmdlets.
This section will help you master the most commonly used commands in network and system administration, guiding you from a "command-line user" to a "system administration expert".
1. Network Management Cmdlets
Test network connectivity:Test-Connection
Similar topingcommand, but the output is an object, supporting more operations.
Test-Connection -ComputerName www.baidu.com -Count 4
Quick version (PowerShell 7+):
Test-NetConnection www.baidu.com
Get local machine IP configuration:Get-NetIPAddress
Get-NetIPAddress
View only IPv4 addresses:
Get-NetIPAddress -AddressFamily IPv4
Can also use the old command for compatibility:
ipconfig
Or a more universal WMI command:
Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where-Object { $_.IPEnabled }
Get current network connection status:Get-NetTCPConnection
List all TCP connections and port statuses (similar tonetstat):
Get-NetTCPConnection
View connection status for a specific port:
Get-NetTCPConnection -LocalPort 80
Manage hostname and DNS cache
Get local machine hostname:
$env:COMPUTERNAME
Clear DNS cache:
Clear-DnsClientCache
2. System Information Query
Get operating system information:Get-ComputerInfo
Get-ComputerInfo
Filter key fields (such as system version, CPU):
Get-ComputerInfo | Select-Object OSName, WindowsVersion, CsProcessors
Get system boot time
(Get-CimInstance -Class Win32_OperatingSystem).LastBootUpTime
Get disk information:Get-Volume
Get-Volume
You can also use:
Get-PSDrive -PSProvider FileSystem
Or:
Get-CimInstance Win32_LogicalDisk | Select-Object DeviceID, VolumeName, Size, FreeSpace
Get BIOS and hardware information
Get-CimInstance -ClassName Win32_BIOS Get-CimInstance -ClassName Win32_ComputerSystem
3. User and Account Management (Local)
Get current user information
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
Or:
$env:USERNAME
View local user list (Windows local accounts)
Get-LocalUser
Disable a user:
Disable-LocalUser -Name "TestUser"
Enable a user:
Enable-LocalUser -Name "TestUser"
Note: Must be run as administrator.
Add a new user (local)
New-LocalUser -Name "NewUser" -Password (Read-Host -AsSecureString "输入密码")
4. System Settings Operations
Set system time zone
Set-TimeZone -Id "China Standard Time"
View supported time zones:
Get-TimeZone -ListAvailable
Set system hostname (requires restart to take effect)
Rename-Computer -NewName "MyNewPC" -Restart
Set system time (requires administrator privileges)
Set-Date -Date "2025-07-22 10:30"
5. Practical Scenario Examples
Example 1: Test connectivity from the local machine to multiple target hosts
"192.168.1.1", "8.8.8.8", "www.baidu.com" | ForEach-Object {
Test-Connection -ComputerName $_ -Count 2
}
Example 2: List currently open TCP ports (status: Listening)
Get-NetTCPConnection | Where-Object { $_.State -eq "Listen" }
Example 3: Generate a system information report
$report = Get-ComputerInfo | Select-Object OSName, OSArchitecture, CsName, CsProcessors, WindowsVersion $report | Out-File -FilePath "C:\SystemReport.txt"
6. Summary
PowerShell provides powerful network and system administration capabilities, far surpassing traditional command-line tools:
- Network operations support testing, monitoring, and configuring IP addresses and ports
- System information commands facilitate generating diagnostic reports and automated audits
- Local account management is suitable for small systems or workgroup environments
- All commands are object-oriented, flexibly composable, and scriptable