Skip to content

Instantly share code, notes, and snippets.

@fearthecowboy
Created February 1, 2019 00:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fearthecowboy/172e7e93328268b3241fddf001d02e52 to your computer and use it in GitHub Desktop.
Save fearthecowboy/172e7e93328268b3241fddf001d02e52 to your computer and use it in GitHub Desktop.
A better new-alias for PowerShell
<#
.SYNOPSIS
Creates a new alias. Better than original New-Alias
.DESCRIPTION
Creates a new Function Alias. Unlike the original New-Alias, this will let you
create an alias that can be a command line or script, in a single command.
.PARAMETER Name
Specifies the new alias. You can use any alphanumeric characters in an alias, but the first character cannot be a number.
.PARAMETER Value
Specifies the name of the cmdlet or command element that is being aliased.
This may be a scriptblock, or a string, or it will consume the remaining
arguments as a command line.
If a scriptblock is given, use of $args will gain access to the arguments
passed to the alias
If a string is given and $args is not in the string, $args will be added to the
end of the command.
If a string is given and $args is present, it will not add $args on the end.
(this preserves functionality with the original new-alias)
.EXAMPLE
Creates an alias "List" which just calls Get-ChildItem with whatever arugments
are passed.
C:\PS> New-Alias List Get-ChildItem
.EXAMPLE
Creates an alias "List" which just calls Get-ChildItem with whatever arugments
are passed.
C:\PS> New-Alias List {Get-ChildItem $args}
.EXAMPLE
Creates an alias "List" which just calls Get-ChildItem with whatever arugments
are passed, and then extracts out the full name of each file
C:\PS> New-Alias List { get-ChildItem $args |% {$_.FullName} }
.EXAMPLE
Creates an alias "d" which just calls dir with whatever arugments
are passed, and tacks on an exclude.
C:\PS> New-Alias d 'dir $args -exclude *.json'
#>
function new-alias([parameter(mandatory=$true, position=0)][string]$name,[parameter(mandatory=$true, position=1, ValueFromRemainingArguments=$true)]$value ) {
if($value[0] -is [Scriptblock]) {
set-Item "function:global:$name" $value[0]
} else {
if( "$value".indexOf('$args') -gt -1) {
set-Item "function:global:$name" ([ScriptBlock]::Create("$value"))
} else {
set-Item "function:global:$name" ([ScriptBlock]::Create("$value `$args"))
}
}
}
@fearthecowboy
Copy link
Author

PS c:\> new-alias d { dir $args |% {$_.FullName} }

PS c:\> d 'C:\Program Files\'                                                                                                           

C:\Program Files\7-Zip
C:\Program Files\AMD
C:\Program Files\Application Verifier
C:\Program Files\Araxis
C:\Program Files\AuthenTec
C:\Program Files\Common Files
C:\Program Files\CPUID
C:\Program Files\DisplayLink Core Software
C:\Program Files\Docker
C:\Program Files\dotnet
C:\Program Files\Everything
C:\Program Files\Git
C:\Program Files\Hyper-V
C:\Program Files\IIS
C:\Program Files\IIS Express
C:\Program Files\Intel
C:\Program Files\internet explorer
C:\Program Files\Java
C:\Program Files\Linux Containers
C:\Program Files\Microsoft ASP.NET Core Runtime Package Store
C:\Program Files\Microsoft Office
C:\Program Files\Microsoft Office 15
C:\Program Files\Microsoft Policy Platform
C:\Program Files\Microsoft SDKs
C:\Program Files\Microsoft SQL Server
C:\Program Files\ModifiableWindowsApps
C:\Program Files\NVIDIA Corporation
C:\Program Files\PackageManagement
C:\Program Files\Realtek
C:\Program Files\VS2010Schemas
C:\Program Files\VS2012Schemas
C:\Program Files\Windows Defender
C:\Program Files\Windows Defender Advanced Threat Protection
C:\Program Files\Windows Mail
C:\Program Files\Windows Media Player
C:\Program Files\Windows Multimedia Platform
C:\Program Files\windows nt
C:\Program Files\Windows Photo Viewer
C:\Program Files\Windows Portable Devices
C:\Program Files\Windows Security
C:\Program Files\WindowsPowerShell

@fearthecowboy
Copy link
Author

new-alias sort-clipboard {get-clipboard | Sort-Object | get-unique | Set-Clipboard} 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment