参考 : 给powershell增加类似于linux的alias功能 - 代码先锋网
设置允许powershell执行脚本
set-executionpolicy remotesigned
然后执行echo $PROFILE 查看profile文件位置
echo $PROFILE
然后创建该文件,填充以下内容
$env:LESSCHARSET='utf-8'
function yun { cd F:\work\yun }
function yun21 { ssh root@192.168.1.21 }
Set-Alias -Name oss -Value F:\Software\ossutil-v1.7.16-windows-amd64\ossutil64.exe
function down {
param (
[string]$u,
[string]$p,
[string]$n
)
if ($p -eq "") {
$p=pwd
}
Write-Host "p:[$u]"
Write-Host "p:[$p]"
Write-Host "p:[$n]"
# 设置下载文件的URL和目标路径
$targetPath = "$p/$n"
# 创建WebClient对象
$client = New-Object System.Net.WebClient
# 下载文件
$client.DownloadFile($u, $targetPath)
# 关闭WebClient连接
$client.Dispose()
}
function p {
$path=pwd
Write-Host "$path"
}
function vi {
param (
[string]$n,
[string]$p
)
if ($p -eq "") {
$p=pwd
}
Start-Process -FilePath "Notepad++.exe" -ArgumentList "$p\$n" -Wait
}
function touch{
param (
[string]$n,
[string]$p
)
# 当前路径 无需传递
if ($p -eq "") {
$p=pwd
}
New-Item -Path "$p\$n" -ItemType File
}
function opwd {
$directory = Get-Location
Start-Process explorer.exe -ArgumentList "/select, `"$directory`""
}
# powershell中执行ll命令
function Get-ChildItemUnix {
Get-ChildItem $Args[0] |
Format-Table Mode, @{N='Owner';E={(Get-Acl $_.FullName).Owner}}, Length, LastWriteTime, @{N='Name';E={if($_.Target) {$_.Name+' -> '+$_.Target} else {$_.Name}}}
}
New-Alias ll Get-ChildItemUnix
# powershell中执行head命令
Function Get-Head {
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string]$Path,
[Parameter(Position = 1)]
[int]$n = 10
)
Get-Content -Path $Path -TotalCount $n
}
Set-Alias -Name head -Value Get-Head
# powershell中执行tail命令
Function Get-Tail {
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string]$Path,
[Parameter(Position = 1)]
[int]$n = 10
)
Get-Content -Path $Path -Tail $n
}
Set-Alias -Name tail -Value Get-Tail
Function Format-FileSize {
Param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[int64]$Size
)
Process {
if ($Size -ge 1TB) {
"{0:N2} TB" -f ($Size / 1TB)
}
elseif ($Size -ge 1GB) {
"{0:N2} GB" -f ($Size / 1GB)
}
elseif ($Size -ge 1MB) {
"{0:N2} MB" -f ($Size / 1MB)
}
elseif ($Size -ge 1KB) {
"{0:N2} KB" -f ($Size / 1KB)
}
else {
"{0:N0} bytes" -f $Size
}
}
}
使用方法
# 查看当前路径
p
# 查看文件列表
ll
# 打开当前目录的文件管理器
opwd
# 创建文件
touch 1.txt
# 编辑文件(用的Notepad++)
vi 1.txt
本文介绍了如何在PowerShell中添加类似于Linux的alias功能,包括设置执行策略、创建常用命令别名如`ll`,`head`,`tail`等,以及管理文件和路径。
1005

被折叠的 条评论
为什么被折叠?



