Windows Server Powershell 常用命令
- 任务自动化框架
- 命令行 shell 和脚本语言
- 基于 .NET Framework/.NET Core
- 面向对象的管道处理
2. Powershell 版本发展
- PowerShell 1.0 (2006)
- PowerShell 2.0 (Windows Server 2008 R2)
- PowerShell 3.0/4.0 (Windows Server 2012/R2)
- PowerShell 5.0/5.1 (Windows Server 2016/2019)
- PowerShell 7.x (跨平台,支持 Windows Server 2022)
注:powershell 中命令不区分大小写
一、系统信息与配置
# 获取操作系统信息
winver
Get-ComputerInfo | Select-Object OsName
# 获取计算机名称
hostname
$env:COMPUTERNAME
# 获取系统启动时间
(Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime
systeminfo | find "Boot Time"
# 获取系统安装日期和时间
(Get-CimInstance Win32_OperatingSystem).InstallDate
# 获取 CPU 信息
Get-CimInstance Win32_Processor | Select-Object Name, NumberOfCores, NumberOfLogicalProcessors
# 获取内存信息
Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum | ForEach-Object {[Math]::Round($_.Sum/1GB,2)}
# 获取硬盘信息
Get-PhysicalDisk | Select-Object FriendlyName, Size, HealthStatus, OperationalStatus
# 获取网络适配器信息
Get-NetAdapter | Where-Object { $_.Status -eq 'Up' } | Select-Object Name, InterfaceDescription, LinkSpeed
二、用户与权限管理
# 获取本地用户列表
Get-LocalUser
# 创建本地用户
New-LocalUser -Name "testuser" -Description "test" -NoPassword
# 修改本地用户密码
Set-LocalUser -Name "testuser" -Password (ConvertTo-SecureString "NewPassword123!" -AsPlainText -Force)
# 删除本地用户
Remove-LocalUser -Name "testuser"
# 获取本地组列表
Get-LocalGroup
# 添加用户到本地组
Add-LocalGroupMember -Group "Administrators" -Member "testuser"
# 从组中移除用户
Remove-LocalGroupMember -Group "Administrators" -Member "testuser"
三、网络配置
# 获取 IP 配置
Get-NetIPConfiguration
# 获取所有网络适配器
Get-NetAdapter
# 设置静态 IP(InterfaceIndex 参数可通过 Get-NetAdapter 命令查看)
New-NetIPAddress -InterfaceIndex 12 -IPAddress 192.168.1.100 -PrefixLength 24 -DefaultGateway 192.168.1.1
# 设置 DNS
Set-DnsClientServerAddress -InterfaceIndex 12 -ServerAddresses ("8.8.8.8","8.8.4.4")
# 测试网络连接
Test-NetConnection -ComputerName www.google.com -Port 80
# ping 测试
Test-Connection -ComputerName 192.168.1.1 -Count 5
# 路由追踪
Test-NetConnection -ComputerName www.google.com -TraceRoute
# 查看监听端口
Get-NetTCPConnection -State Listen | Sort-Object LocalPort | Select-Object LocalAddress, LocalPort, OwningProcess
netstat -ano
四、服务与进程管理
# 获取所有服务
Get-Service
# 启动服务
Start-Service -Name "Spooler"
# 停止服务
Stop-Service -Name "Spooler" -Force
# 重启服务
Restart-Service -Name "Spooler"
# 设置服务启动类型
Set-Service -Name "Spooler" -StartupType Automatic
# 获取运行中的进程
Get-Process | Sort-Object CPU -Descending | Select-Object -First 10
# 结束进程
Stop-Process -Name "notepad" -Force
# 根据 ID 结束进程
Stop-Process -ID 1234 -Force
# 查找特定进程
Get-Process | Where-Object { $_.ProcessName -like "*chrome*" }
五、文件与目录操作
# 列出目录内容
Get-ChildItem -Path C:\Windows -Recurse -Depth 2
# 创建目录
New-Item -Path "C:\Temp\NewFolder" -ItemType Directory
# 删除目录
Remove-Item -Path "C:\Temp\OldFolder" -Recurse -Force
# 复制文件
Copy-Item -Path "C:\file.txt" -Destination "D:\backup\file.txt" -Force
# 获取文件后20行内容
Get-Content -Path "C:\logs\app.log" -Tail 20
# 获取文件权限
Get-Acl -Path "C:\SensitiveData" | Format-List
# 设置文件权限
$acl = Get-Acl -Path "C:\SensitiveData"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("testuser","FullControl","Allow")
$acl.SetAccessRule($accessRule)
Set-Acl -Path "C:\SensitiveData" -AclObject $acl
六、Active Directory 管理
# 获取 AD 用户
Get-ADUser -Filter * -Properties * | Select-Object Name, SamAccountName, Enabled
# 创建 AD 用户
New-ADUser -Name "John Doe" -SamAccountName "jdoe" -AccountPassword (ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force) -Enabled $true
# 禁用用户
Disable-ADAccount -Identity "jdoe"
# 解锁用户
Unlock-ADAccount -Identity "jdoe
# 获取 AD 组
Get-ADGroup -Filter * | Select-Object Name, GroupScope
# 创建 AD 组
New-ADGroup -Name "Finance" -GroupScope Global -Path "OU=Groups,DC=domain,DC=com"
# 添加用户到 AD 组
Add-ADGroupMember -Identity "Finance" -Members "jdoe"
七、Windows 更新管理
# 检查更新
Get-WindowsUpdate
# 安装所有更新
Install-WindowsUpdate -AcceptAll -AutoReboot
# 获取已安装更新
Get-HotFix | Sort-Object InstalledOn -Descending | Select-Object -First 10
八、远程管理
# 启用远程管理
Enable-PSRemoting -Force
# 创建远程会话
Enter-PSSession -ComputerName Server01 -Credential (Get-Credential)
# 执行远程命令
Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Service } -Credential (Get-Credential)
# 文件传输
Copy-Item -FromSession $session -Path "C:\RemoteFile.txt" -Destination "C:\LocalFile.txt"
# 向远程发送文件
Copy-Item -ToSession $session -Path "C:\LocalFile.txt" -Destination "C:\RemoteFile.txt"
九、计划任务
# 获取所有计划任务
Get-ScheduledTask | Where-Object { $_.State -eq "Ready" }
# 创建计划任务
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -File C:\Scripts\backup.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At 3am
Register-ScheduledTask -TaskName "Nightly Backup" -Action $action -Trigger $trigger -User "SYSTEM"
十、日志管理
# 获取应用程序日志
Get-EventLog -LogName Application -Newest 20
# 查询特定事件 ID
Get-WinEvent -FilterHashtable @{LogName='System'; ID=1074} | Select-Object TimeCreated, Message
# 清除日志
Clear-EventLog -LogName Application
十一、性能监控
# 实时监控 CPU 使用率
Get-Counter '\Processor(_Total)\% Processor Time' -Continuous
# 实时监控剩余可使用内存
Get-Counter '\Memory\Available MBytes' -Continuous
十二、关机和重启
# 关机
shutdown /s /f /t 0 # /s 关闭计算机(shutdown);/f 强制关闭(force),强制终止所有正在运行的应用程序,不显示警告对话框;/t 0 延迟时间为 0 秒(立即执行)
# 重启
shutdown /r /f /t 0 # /r 重启计算机(restart)
十三、Powershell 调用 cmd 命令
# 某些命令只在 cmd 中有,在 powershell 命令中添加 cmd /c 前缀即可调用 cmd 命令

浙公网安备 33010602011771号