Is there a command for checking alias existence in PowerShell?
You can use Test-Path
for this:
PS C:\> test-path alias:l*
True
PS C:\> test-path alias:list*
False
Just use:
if (Test-Path alias:list*) { ... }
or
if(!(Test-Path alias:list*)) { ... }
if you want to do something when the alias doesn't exist.
As others wrote, Get-Alias
works as well, but Test-Path
will avoid building the list of aliases so might be every so slightly faster if you care about that.
Further explanation:
Powershell uses path names to access a lot more than just files, so while C:
prefix lets you access files on drive C, alias:
lets you access aliases, and other qualifers let you access things like functions, commands, registry keys, security certificates, and so on. So here, alias:l*
is a wildcard pathname which will match all aliases that begin with the letter l
in exactly the same way that C:l*
would match all files beginning with l
in the root of drive C.
Test-Path
is a commandlet which tests whether the path refers to an existing object with a True result only if at least one existing object matches that path. So normally you would use it to test whether a file exists but equally you can test for existence of anything that is addressable with a suitable qualifier.
Yes, use the Get-Alias
command.
Example:
Get-Alias -Name spps;
# If statement example
if (Get-Alias -Name spps) {
# Do something here
}
To get a list of all commands that deal with aliases, use Get-Command
Get-Command -Name *alias;
I think Get-Alias is what you are looking for.
MS Documentation: http://technet.microsoft.com/en-gb/library/ee176839.aspx
Example for IF:
$ar = get-alias -name f*
if($ar.count -lt 1){ do stuff }