跳到主要内容

常用 Cmdlet

PowerShell 提供了数百个内置 cmdlet,掌握常用命令是高效使用 PowerShell 的关键。本章介绍日常管理中最常用的 cmdlet。

获取帮助

Get-Help

获取命令的帮助信息:

Get-Help Get-Process
Get-Help Get-Process -Detailed
Get-Help Get-Process -Full
Get-Help Get-Process -Examples

在线查看帮助:

Get-Help Get-Process -Online

搜索帮助主题:

Get-Help *process*
Get-Help about_variables

更新帮助内容:

Update-Help

Get-Command

查找可用的命令:

Get-Command
Get-Command *process*
Get-Command -Noun Process
Get-Command -Verb Get

按模块查找:

Get-Command -Module Microsoft.PowerShell.Management

进程管理

Get-Process

获取进程信息:

Get-Process
Get-Process chrome
Get-Process -Id 1234
Get-Process | Where-Object { $_.CPU -gt 100 }

Stop-Process

停止进程:

Stop-Process -Name notepad
Stop-Process -Id 1234
Stop-Process -Name chrome -Force

通过管道停止:

Get-Process chrome | Stop-Process

Start-Process

启动进程:

Start-Process notepad
Start-Process "C:\Program Files\Google\Chrome\Application\chrome.exe"
Start-Process notepad -Verb RunAs

带参数启动:

Start-Process chrome -ArgumentList "https://www.google.com"

服务管理

Get-Service

获取服务信息:

Get-Service
Get-Service -Name "WinRM"
Get-Service | Where-Object { $_.Status -eq "Running" }

按显示名称搜索:

Get-Service -DisplayName "*remote*"

Start-Service / Stop-Service

启动和停止服务:

Start-Service -Name "WinRM"
Stop-Service -Name "WinRM"

通过管道操作:

Get-Service -Name "WinRM" | Start-Service
Get-Service | Where-Object { $_.Status -eq "Running" } | Stop-Service

Restart-Service

重启服务:

Restart-Service -Name "WinRM"
Restart-Service -Name "WinRM" -Force

Set-Service

修改服务属性:

Set-Service -Name "WinRM" -StartupType Automatic
Set-Service -Name "WinRM" -Status Running
Set-Service -Name "WinRM" -DisplayName "Windows Remote Management"

New-Service

创建新服务:

New-Service -Name "MyService" -BinaryPathName "C:\Services\MyService.exe" -DisplayName "My Custom Service" -StartupType Automatic

文件系统操作

Get-ChildItem

列出目录内容:

Get-ChildItem
Get-ChildItem C:\Windows
Get-ChildItem -Path C:\ -Recurse -ErrorAction SilentlyContinue

筛选文件:

Get-ChildItem -Filter "*.txt"
Get-ChildItem -Include "*.txt", "*.log" -Recurse
Get-ChildItem -Exclude "*.tmp"

只显示文件或目录:

Get-ChildItem -File
Get-ChildItem -Directory

隐藏文件:

Get-ChildItem -Force
Get-ChildItem -Hidden

别名:dir, ls, gci

New-Item

创建文件或目录:

New-Item -Path "C:\Temp\test.txt" -ItemType File
New-Item -Path "C:\Temp\NewFolder" -ItemType Directory

创建并写入内容:

New-Item -Path "C:\Temp\test.txt" -ItemType File -Value "Hello World"

Copy-Item

复制文件或目录:

Copy-Item -Path "C:\Source\file.txt" -Destination "C:\Dest\"
Copy-Item -Path "C:\Source\" -Destination "C:\Dest\" -Recurse

Move-Item

移动或重命名:

Move-Item -Path "C:\Temp\old.txt" -Destination "C:\Temp\new.txt"
Move-Item -Path "C:\Source\" -Destination "C:\Dest\"

Remove-Item

删除文件或目录:

Remove-Item -Path "C:\Temp\test.txt"
Remove-Item -Path "C:\Temp\Folder" -Recurse -Force

Get-Content / Set-Content

读取和写入文件内容:

Get-Content "C:\Temp\test.txt"
Get-Content "C:\Temp\test.txt" -TotalCount 10
Get-Content "C:\Temp\test.txt" -Tail 10

Set-Content -Path "C:\Temp\test.txt" -Value "New content"
Add-Content -Path "C:\Temp\test.txt" -Value "Appended line"

Test-Path

检查路径是否存在:

Test-Path "C:\Windows"
Test-Path "C:\Temp\test.txt"

检查路径类型:

Test-Path "C:\Windows" -PathType Container
Test-Path "C:\Windows\notepad.exe" -PathType Leaf

网络操作

Test-Connection

测试网络连接:

Test-Connection -ComputerName "google.com"
Test-Connection -ComputerName "192.168.1.1" -Count 4
Test-Connection -ComputerName "google.com" -Quiet

Test-NetConnection

高级网络测试:

Test-NetConnection -ComputerName "google.com"
Test-NetConnection -ComputerName "google.com" -Port 443
Test-NetConnection -ComputerName "smtp.gmail.com" -Port 587

Invoke-WebRequest

发送 HTTP 请求:

$response = Invoke-WebRequest -Uri "https://api.github.com"
$response.Content
$response.StatusCode

下载文件:

Invoke-WebRequest -Uri "https://example.com/file.zip" -OutFile "C:\Downloads\file.zip"

Invoke-RestMethod

调用 REST API:

$response = Invoke-RestMethod -Uri "https://api.github.com/users/github"
$response.login

POST 请求:

$body = @{
name = "test"
value = "example"
} | ConvertTo-Json

Invoke-RestMethod -Uri "https://api.example.com/data" -Method Post -Body $body -ContentType "application/json"

系统信息

Get-ComputerInfo

获取计算机信息:

Get-ComputerInfo
Get-ComputerInfo -Property "OsName", "WindowsVersion", "CsName"

Get-CimInstance

查询 WMI 类:

Get-CimInstance -ClassName Win32_OperatingSystem
Get-CimInstance -ClassName Win32_Processor
Get-CimInstance -ClassName Win32_LogicalDisk

Get-Volume

获取磁盘卷信息:

Get-Volume
Get-Volume -DriveLetter C

Get-PSDrive

获取 PowerShell 驱动器:

Get-PSDrive
Get-PSDrive -PSProvider FileSystem

用户和组管理

Get-LocalUser

获取本地用户:

Get-LocalUser
Get-LocalUser -Name "Administrator"

New-LocalUser

创建本地用户:

$password = Read-Host -AsSecureString
New-LocalUser -Name "NewUser" -Password $password -FullName "New User" -Description "Test account"

Add-LocalGroupMember

添加用户到组:

Add-LocalGroupMember -Group "Administrators" -Member "NewUser"

Remove-LocalUser

删除用户:

Remove-LocalUser -Name "NewUser"

历史记录

Get-History

获取命令历史:

Get-History
Get-History -Count 10

Invoke-History

执行历史命令:

Invoke-History -Id 5

Clear-History

清除历史:

Clear-History

实用技巧

命令别名

查看别名:

Get-Alias
Get-Alias -Definition Get-ChildItem

创建别名:

Set-Alias -Name ll -Value Get-ChildItem

管道变量

将管道结果存储到变量:

Get-Process -OutVariable processes
$processes | Measure-Object

Tee-Object

同时输出到多个目标:

Get-Process | Tee-Object -FilePath "processes.txt" | Select-Object -First 5

Out-GridView

图形化显示结果:

Get-Process | Out-GridView
Get-Process | Out-GridView -Title "进程列表" -OutputMode Single

下一步

掌握了常用 cmdlet 后,接下来学习 文件操作,深入了解文件系统管理。