Wednesday, November 16, 2016

PowerShell v3 in a Year Day 3 about Aliases

,
Alises, in PowerShell, are a fairly straightforward concept. You can get the full details yourself with this command:
Get-Help about_Aliases -Full
In short, Aliases "[d]escribe how to use alternate names for cmdlets and commands in Windows
PowerShell." You can use them for cmdlets or command elements: functions, scripts, files or executables. When you type the alias, along with any standard parameters and arguments, it will work just as expected. To create a new one simply use the New-Alias cmdlet. The simplest form takes two arguments for the -Name and -Value parameters. -Name refers to the alias you intend to use and -Value is the original cmdlet or command element you are aliasing.
New-Alias -Name gfs -Vale Get-FileSignature
When you create aliases for regular executables you no longer need to fully qualify the executable path. Simply type the alias and voila, there you have it.

There are several built-in aliases the come as a part of PowerShell straight out of the box. To get an idea of what these aliases are simply run this command:
Get-Alias
when it executes it will return a list like this shortened example. I had not seen this modified syntax in v3 yet. I like it:
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
When working with aliases themselves, there are five main cmdlets to be aware of:

  • Get-Alias. Gets all the aliases in the current session.
  • New-Alias. Creates a new alias.
  • Set-Alias. Creates or changes an alias.
  • Export-Alias. Exports one or more aliases to a file.
  • Import-Alias. Imports an alias file into Windows PowerShell
When aliases are created they only exist in your current session. As soon as the session ends, the aliases disappear. So, you can add any important aliases to your profile for regular reuse. Getting details on alises can be narrowed down with wildcards and -Detailed. For instance, if you want all the aliases in your current session whose names begin with p, use this:
Get-Alias p* -Detailed
 One last thing to note about aliases: PowerShell has a provider for them. So, you can work with them just like any other provider. Since its a provider, I can set it and do what i need to. For example,
PS C:Userswsteele> cd alias:
PS Alias:> dir

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

0 comments to “PowerShell v3 in a Year Day 3 about Aliases”

Post a Comment

 

Computer Info Copyright © 2016 -- Powered by Blogger