Powershell 常用命令集合
PowerShell 常用命令全面汇总
通用命令
| 命令 | 用途 | 常用选项 | 示例 |
|---|---|---|---|
| Get-Help | 查看命令帮助 | -Full, -Examples | Get-Help Get-Process -Examples |
| Get-Command | 列出所有命令 | -Name, -Module | Get-Command -Name *service* |
| Get-Alias | 查看别名 | -Name | Get-Alias -Name ls |
| Get-History | 查看历史命令 | -Count | Get-History -Count 20 |
| Clear-History | 清除历史记录 | — | Clear-History |
文件与目录管理
| 命令 | 用途 | 常用选项 | 示例 |
|---|---|---|---|
| Get-ChildItem | 列出目录内容 | -Recurse, -Filter | Get-ChildItem -Path .\ -Recurse -Filter *.log |
| New-Item | 创建文件/文件夹 | -ItemType | New-Item -Path .\test.txt -ItemType File |
| Copy-Item | 复制文件/目录 | -Recurse, -Force | Copy-Item .\file.txt D:\Backup\ |
| Move-Item | 移动文件/目录 | — | Move-Item .\file.txt D:\Archive\ |
| Remove-Item | 删除文件/目录 | -Recurse, -Force | Remove-Item .\old_folder -Recurse -Force |
| Rename-Item | 重命名文件/目录 | -NewName | Rename-Item .\old.txt new.txt |
进程与服务管理
| 命令 | 用途 | 常用选项 | 示例 |
|---|---|---|---|
| Get-Process | 查看进程 | -Name | Get-Process chrome |
| Stop-Process | 结束进程 | -Id, -Name | Stop-Process -Name chrome |
| Get-Service | 查看服务 | -Name, -Status | Get-Service -Name wuauserv |
| Start-Service | 启动服务 | -Name | Start-Service -Name wuauserv |
| Stop-Service | 停止服务 | -Name | Stop-Service -Name wuauserv |
| Restart-Service | 重启服务 | -Name | Restart-Service -Name wuauserv |
网络与远程管理
| 命令 | 用途 | 常用选项 | 示例 |
|---|---|---|---|
| Test-Connection | 测试连通性 (ping) | -ComputerName, -Count | Test-Connection google.com -Count 4 |
| Invoke-WebRequest | 获取网页内容 | -Uri, -OutFile | Invoke-WebRequest -Uri https://example.com -OutFile page.html |
| Invoke-Command | 远程执行命令 | -ComputerName, -ScriptBlock | Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Service } |
| Enter-PSSession | 进入远程会话 | -ComputerName | Enter-PSSession -ComputerName Server01 |
| Exit-PSSession | 退出远程会话 | — | Exit-PSSession |
用户与权限管理
| 命令 | 用途 | 常用选项 | 示例 |
|---|---|---|---|
| Get-LocalUser | 查看本地用户 | -Name | Get-LocalUser -Name Administrator |
| New-LocalUser | 创建本地用户 | -Name, -Password | New-LocalUser -Name TestUser -Password (Read-Host -AsSecureString) |
| Set-LocalUser | 修改用户属性 | -Name, -Password | Set-LocalUser -Name TestUser -Password (Read-Host -AsSecureString) |
| Remove-LocalUser | 删除本地用户 | -Name | Remove-LocalUser -Name TestUser |
| Get-LocalGroup | 查看本地组 | -Name | Get-LocalGroup |
| Add-LocalGroupMember | 添加组成员 | -Group, -Member | Add-LocalGroupMember -Group Administrators -Member TestUser |
| Remove-LocalGroupMember | 移除组成员 | -Group, -Member | Remove-LocalGroupMember -Group Administrators -Member TestUser |
脚本与任务管理
| 命令 | 用途 | 常用选项 | 示例 |
|---|---|---|---|
| Set-ExecutionPolicy | 设置脚本执行策略 | Restricted, RemoteSigned | Set-ExecutionPolicy RemoteSigned |
| Start-Job | 后台启动任务 | -ScriptBlock | Start-Job -ScriptBlock { Get-Process } |
| Get-Job | 查看后台任务 | -Id, -Name | Get-Job |
| Receive-Job | 获取任务结果 | -Id, -Name | Receive-Job -Id 1 |
| Stop-Job | 停止后台任务 | -Id, -Name | Stop-Job -Id 1 |
