Home Best Practice Scripts Do not use Aliases in Scripts
 

Do not use Aliases in Scripts

Aliases are secondary names for commands. PowerShell comes with a number of predefined alias names that you can view with Get-Alias.

PS> Get–Alias

CommandType     Name                                               ModuleName                      
–––––––––––     ––––                                               ––––––––––                      
Alias           % –> ForEach–Object                                                                
Alias           ? –> Where–Object                                                                  
Alias           ac –> Add–Content                                                                  
Alias           asnp –> Add–PSSnapin                                                               
Alias           cat –> Get–Content                                                                 
Alias           cd –> Set–Location                                                                 
Alias           chdir –> Set–Location                                                              
Alias           clc –> Clear–Content                                                               
Alias           clear –> Clear–Host                                                                
Alias           clhy –> Clear–History                                                              
Alias           cli –> Clear–Item                                                                  
Alias           clp –> Clear–ItemProperty                                                          
Alias           cls –> Clear–Host                                                                  
Alias           clv –> Clear–Variable                                                              
Alias           cnsn –> Connect–PSSession                                                          
Alias           compare –> Compare–Object                                                          
Alias           copy –> Copy–Item                                                                  
Alias           cp –> Copy–Item                                                                    
Alias           cpi –> Copy–Item                                                                   
Alias           cpp –> Copy–ItemProperty                                                           
Alias           curl –> Invoke–WebRequest                                                          
Alias           cvpa –> Convert–Path                                                               
Alias           dbp –> Disable–PSBreakpoint                                                        
Alias           del –> Remove–Item                                                                 
Alias           diff –> Compare–Object                                                             
Alias           dir –> Get–ChildItem                                                               
Alias           dnsn –> Disconnect–PSSession                                                       
Alias           ebp –> Enable–PSBreakpoint                                                         
Alias           echo –> Write–Output                                                               
Alias           epal –> Export–Alias                                                               
Alias           epcsv –> Export–Csv                                                                
Alias           epsn –> Export–PSSession 

  Listing all defined aliases

Aliases serve two purposes: there are "historic" aliases such as "dir" or "ls", aiming to make life easier for people transitioning from other shells to PowerShell. And aliases are much shorter than command names, so they help speed up work with the interactive PowerShell console in ever-day life.

Never Use Aliases in Scripts

Aliases are made for the interactive PowerShell console – they are perfectly ok in code that won't live for very much longer than a couple of milliseconds.

Aliases are NOT made for PowerShell scripts – for code that lasts months or even years, and may need to be reviewed in the distant future.

This is why you should never use aliases in PowerShell scripts. In scripts, always make sure you replace aliases with the commands they point to.

Here is how you can find out the true command an alias points to:

PS> Get–Command dir

CommandType     Name                                               ModuleName                      
–––––––––––     ––––                                               ––––––––––                      
Alias           dir –> Get–ChildItem                                                               

PS> Get–Command cd

CommandType     Name                                               ModuleName                      
–––––––––––     ––––                                               ––––––––––                      
Alias           cd –> Set–Location                                                                 

PS> 

  Finding the command "behind" the aliases "dir" and "cd"

Why Aliases Can Be Dangerous

So why is it unadvisable to use aliases in PowerShell scripts? First of all, aliases make scripts inconsistent. With aliases, you may have a mixture of command names in a script that all do the same.

More importantly, aliases are made for the interactive PowerShell user, and it is perfectly ok for a user to change or remove aliases.

You can even delete and redefine built-in aliases. If your script uses the alias "dir", and a user changed it to "format", then your script most likely will not do what you intended it to do.

Deleting And Manipulating Aliases

Here is a very DANGEROUS piece of code, illustrating why it can be risky to depend on aliases. This script would delete the built-in alias "dir", and instead create a new one that points to Remove-Item:

  

# dangerous! dangerous! dangerous!
Remove-Item -Path Alias:dir
Set-Alias -Name dir -Value Remove-Item

  Dangerous piece of code, turning "dir" into a "del"

Once executed, whenever you use "dir", PowerShell will no longer list the folder but actually delete it:

PS> md c:\testfolder

    Directory: C:\

Mode                LastWriteTime     Length Name                                                  
––––                –––––––––––––     –––––– ––––                                                  
d––––        21.01.2014     21:23            testfolder                                            

PS> Test–Path –Path c:\testfolder
True

PS> dir C:\testfolder

PS> Test–Path –Path c:\testfolder
False

PS> 

  "dir" just deleted the folder instead of listing its content

Fortunately, manipulating an alias is really not a significant attack vector, because it is not possible for one user to manipulate the aliases of another (except for Administrators). And many cmdlets have additional security checks built-in, so Remove-Item would ask for confirmation if the folder contained subfolders.

But at minimum, you can hurt yourself, and when you ship your scripts to someone else, you never know what that person did to their alises. So do not use aliases in PowerShell scripts for good.