Is there a PowerShell command that allows you to check to see if an alias exists or not?
(I have been looking around on the web, and it doesn't look like it.)
Is there a PowerShell command that allows you to check to see if an alias exists or not?
(I have been looking around on the web, and it doesn't look like it.)
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.
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 }
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;
There is a provider just for Aliases, and an Alias: location, so you can also use Get-Childitem.
Get-Childitem alias:
Another option if you are just wanting to set an alias if it does not already exist is to use the -Force
parameter in Set-Alias
, so you do not get an error saying the alias already exists:
Set-Alias "foo" MyFooFunction -Force
Things like if(!(Get-Alias -Name foo))
error if the alias does not exist.
(tested in version 5)
Here is how I use the above (Big thanks to all!):
If ((Test-Path alias:foo) -eq $false) {New-Alias -name foo -value Get-Foo}
This is at the bottom of every script that is or will become a module. It puts an alias on the user's system without throwing an error or overwriting an existing alias if the alias already exists.
Inspired from answers provided by Duncan and Dana, I have created the following function. i recommend adding it to the file pointed by your PowerShell's $PROFILE variable.
function New-AliasSafe {
param(
[Parameter(Mandatory=$true)]
[string]
$Name,
[Parameter(Mandatory=$true)]
[string]
$Value
)
if (!(Test-Path alias:$Name)) {
New-Alias -Name $Name -Value $Value -Scope Global
Write-Verbose "Alias $Name created successfully!"
} else {
Write-Verbose "Another alias with $Name already exists!"
}
}
Now you can just call this function whenever you want to import an alias.
New-AliasSafe -Name wo -Value Write-Output
If you want to know whether the alias was successfully created or not, you can call this function like this instead.
New-AliasSafe -Name wo -Value Write-Output -Verbose
Just wanted to add yet another way to achieve this..
$ErrorActionPreference = 'Stop'
$ee = @() # Exception catcher
$Newalias = 'wv'
try{
New-Alias -Name $Newalias -Value Write-Verbose | Out-Null
}
catch{$ee += $_}
if (($ee -ne $null) -and ($ee.CategoryInfo.Category.ToString() -match 'ResourceExists')){Write-Warning "Alias $Newalias already exists";break}
else{Write-Verbose 'Continue on'}
Catch the exception and check if that matches the condition for the Boolean logic of existence. I've found this works well unless the exception is different than you were expecting.