-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.ps1
33 lines (33 loc) · 1.22 KB
/
Utils.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<#
.SYNOPSIS
Register a new alias
.DESCRIPTION
It supports providing string statements, if a single string is provided, it will call `Set-Alias` directly, if a statement is provided, it will automatically construct a dynamic function with the name `<Name>AliasFunction` and then call `Set-Alias`, you can call `Set-Alias` with `Get-Command -CommandType Function -Name "*AliasFunction"` to see all the alias registered in this way.
.PARAMETER Name
The alias name.
.PARAMETER Value
The alias content, it can be a string or a statement.
.EXAMPLE
Register-Alias hello "echo 'Hello World!'"
Typing "hello" to output a string "Hello World!".
.EXAMPLE
Register-Alias i "cd ~/Projects/$($args[0])"
Recieve a parameter $args[0] from the command line, and then change the current directory to ~/Projects/$($args[0]).
.INPUTS
None.
.OUTPUTS
None.
.LINK
https:/aliuq/Register-Completion
#>
function Register-Alias {
Param([string]$Name, [string]$Value)
if (($Value -Split ' ').Count -le 1) {
Set-Alias $Name $Value -scope global
}
else {
$fullName = $Name + "AliasFunction"
Set-Item "Function:\global:$fullName" -Value ([scriptblock]::Create($Value))
Set-Alias $Name $fullName -scope global
}
}