PowerShell 的命令(Cmdlet)通常遵循 Verb-Noun
命名规范,但也可以使用 简写(Alias) 来更快地执行操作。以下是一些常见的 PowerShell 命令及其简写(别名):
1. 常见 PowerShell Cmdlet 的简写
完整命令 | 简写(Alias) | 作用 |
---|---|---|
Get-ChildItem | gci , ls , dir | 列出目录内容 |
Set-Location | cd , chdir , sl | 切换目录 |
Get-Location | pwd | 显示当前目录 |
Clear-Host | cls , clear | 清屏 |
Get-Help | help , man | 获取帮助信息 |
Get-Process | gps , ps | 获取进程列表 |
Stop-Process | spps , kill | 终止进程 |
Start-Process | saps , start | 启动进程 |
Get-Service | gsv | 列出服务 |
Stop-Service | spsv | 停止服务 |
Start-Service | sasv | 启动服务 |
Restart-Service | resv | 重启服务 |
Get-Content | gc , cat , type | 读取文件内容 |
Set-Content | sc | 写入文件内容 |
Add-Content | ac | 追加文件内容 |
Copy-Item | copy , cp | 复制文件或目录 |
Move-Item | move , mv | 移动文件或目录 |
Remove-Item | del , erase , rd , rm , rmdir | 删除文件或目录 |
New-Item | ni | 创建新文件或目录 |
Rename-Item | ren , rni | 重命名文件或目录 |
Test-Path | tp | 检查路径是否存在 |
Get-Alias | gal | 获取命令别名 |
Set-Alias | sal | 创建自定义别名 |
Get-Command | gcm | 获取命令列表 |
Get-Module | gmo | 列出已加载的模块 |
Import-Module | ipmo , impo | 导入模块 |
Remove-Module | rpmo | 卸载模块 |
Get-History | h , history , ghy | 获取历史命令 |
Invoke-Expression | iex | 运行字符串命令 |
Invoke-Command | icm | 远程执行命令 |
Write-Output | echo , write | 输出文本 |
Write-Host | 无 | 输出到屏幕 |
Select-String | sls | 在文本中搜索字符串 |
Sort-Object | sort | 排序 |
Measure-Object | measure | 计算文件或字符串 |
ConvertTo-Json | ctj | 转换为 JSON |
ConvertFrom-Json | cfj | 解析 JSON |
Export-Csv | epcsv | 导出 CSV 文件 |
Import-Csv | ipcsv | 导入 CSV 文件 |
2. 获取更多别名
你可以使用以下命令列出所有别名:
Get-Alias
如果你想查看某个命令的别名,比如 Get-ChildItem
:
Get-Alias -Definition Get-ChildItem
反过来,如果你想知道 ls
实际上代表什么:
Get-Alias ls
3. 创建自定义别名
如果你经常使用某个命令,可以为它创建自定义别名:
Set-Alias -Name mycmd -Value Get-Process
这样你只需输入 mycmd
,就能执行 Get-Process
。
如果要让别名永久生效,可以把 Set-Alias
命令加到 PowerShell 配置文件 ($PROFILE
) 中:
notepad $PROFILE
在文件中添加:
Set-Alias ll Get-ChildItem
保存后,重新启动 PowerShell 即可。
4. 结合管道使用
PowerShell 的强大之处在于 管道(|
),可以组合多个命令:
gps | sort CPU -Descending | select -First 5
上述命令会列出 占用 CPU 最高的 5 个进程。
这些 PowerShell 简写命令可以帮助你更快地执行各种操作,提高工作效率! 🚀