添加别名
为自己的 powershell
添加别名
平时使用powsershell
来处理工作时,很多命令回重复重复再重复,比如ssh登入服务器,快速获取当前大于1G的文件,等等等
powershell
本身页自带了类似ls
cp
等别名来方便用户
1、通过预加载 *.ps1
脚本
设置自己的别名
powershell
可以配置一个环境变量脚本${$profile}\xxxx.ps1
配置文件,每次启动时都会读取运行一次,类似linux的.brash
# 首先检测当前是否已有配置文件
Test-Path $profile # True | False
# 如果不存在,可以使用命令创建
new-item -path $profile -itemtype file -force
添加别名函数到配置文件
# 获取当前配置文件位置
echo $PROFILE
# 直接打开配置文件
start $PROFILE
# 语法
function name { .../ }
# 示例
function cps_ssh { ssh root@xx.xx.xxx.xx }
设置好后重新打开powershell,输入对应的别名,即可执行对应命令
常用别名函数
- git-load
function git-load {
git fetch --all
git reset --hard origin/master
git pull origin master
}
- git-push
function git-push {
git add .
git commit -m "$msg"
git push origin master
}
2、Set-Alias
- 语法
Set-Alias -Name xxxx -Value xxxxx
Set-Alias <别名> <命令>
# 显示当前别名列表
Alias
Get-Alias
使用示例
- 快速访问服务器
function cps-server {
ssh cps@x.x.x.x -p xxxx
}
function cps-server@root {
ssh root@x.x.x.x -p xxxx
}
function hongqi {
ssh hongqi@x.x.x.x -p xxxx
}
function hongqi@root {
ssh root@x.x.x.x -p xxxx
}
# 使用
$ cps-server
- 获取历史记录
function cps-get-history {
$his = Get-Content (Get-PSReadLineOption).HistorySavePath
$n = $his.Length
$out = @()
for($i=0;$i -lt $n;$i++)
{
$out = $out + "$i $($his[$i])"
}
$out = $out + "history count: $n"
return $out
}
# 使用
$ cps-get-history
- 清楚历史记录
function cps-clean(){
$his = Get-Content (Get-PSReadLineOption).HistorySavePath
$n = $his.Length
$res = Remove-Item (Get-PSReadlineOption).HistorySavePath
return "clear $n history done ! "
}
# 使用
$ cps-clean