PowerShell Cmdlet Deep Dive: New-Alias
Welcome back to Wahmans PowerShell Blog! Today we are going to cover a simple yet powerful cmdlet that can streamline your command-line workflows — New-Alias.
What is New-Alias?
The New-Alias cmdlet allows you to create a new alias for a PowerShell cmdlet or function. Aliases are shorthand names that you can use instead of typing out full cmdlet names, making your scripting more efficient. According to Microsoft:
New-Alias: Creates a new alias.
Aliases are great for repeated use, easing the transition from other shells, or even just customizing your own PowerShell experience.
Usage Syntax
New-Alias [-Name] <string> [-Value] <string> [-Description <string>] [-Force] [-Passthru] [-Scope <string>]
Examples from Beginner to Advanced
1. Beginner: Create an Alias for Get-ChildItem
If you’re used to Linux commands like ls, you can map ls to Get-ChildItem.
New-Alias -Name ls -Value Get-ChildItem
Now you can simply type ls in PowerShell to list directory contents!
2. Intermediate: Alias for a Custom Function
Let’s say you have a function called Get-SystemInfo that gathers system information. You can create an alias for it.
function Get-SystemInfo {
Get-ComputerInfo | Select-Object OSName, CsTotalPhysicalMemory
}
New-Alias -Name sysinfo -Value Get-SystemInfo
Now just type sysinfo to execute your custom function.
3. Advanced: Temporarily Overriding Built-in Alias
PowerShell comes with built-in aliases like gc for Get-Content. You can override it temporarily.
New-Alias -Name gc -Value Get-Clipboard -Force
This changes gc to paste from clipboard instead. Be aware that this only lasts for your session unless you add it to your profile.
4. Advanced: Scoped Aliases
You can set scope for your alias, ensuring it only exists in a specific scope (e.g., a script or a function scope).
function Test-AliasScope {
New-Alias -Name whoami -Value whoami.exe -Scope Local
whoami
}
Test-AliasScope
whoami # This will only work inside the function scope
By defining the scope, your alias won’t interfere with global aliases outside the function.
Wrapping Up
The New-Alias cmdlet can save you time, help with cross-platform comfort (like mapping bash commands), or even streamline your workflow by mapping frequently used cmdlets and functions. Whether you’re scripting occasionally or living in PowerShell daily, custom aliases are a handy feature to have in your toolbox.
Happy scripting, and I will see you in the next post!
Leave a Reply