Speed up your PowerCLI cmdlet typing with aliases
January 14, 2012 3 Comments
If you are like me and managing a VMware vSphere environment is your daily job, and you want to automate your job as much as possible, you probably always have a PowerCLI session open. PowerCLI cmdlet names are sometimes pretty long. Of course you can use tab completion. But wouldn’t it be nice if you had short aliases for the PowerCLI cmdlets?
The number of PowerCLI convenience aliases
PowerCLI is based on PowerShell and in PowerShell there are a lot of so called “convenience aliases”. E.g. gm for Get-Member and gwmi for Get-WmiObject. How many of these “convenience aliases” are there for PowerCLI?
Let’s find out. I made the short PowerShell script in listing 1 to show you all the PowerCLI aliases.
$Alias = Get-Alias Get-VICommand | ForEach-Object { $VICommand = $_ $Alias | Where-Object {$_.Definition -eq $VICommand.Name} } | Format-Table -AutoSize
Listing 1. PowerShell script to show all the PowerCLI aliases.
If I run the script from listing 1 in a PowerCLI 5.0.1 session, I get the following output:
CommandType Name Definition ----------- ---- ---------- Alias Get-ESX Connect-VIServer Alias Get-VC Connect-VIServer Alias Get-VIServer Connect-VIServer Alias Get-VIToolkitConfiguration Get-PowerCLIConfiguration Alias Get-VIToolkitVersion Get-PowerCLIVersion Alias Set-VIToolkitConfiguration Set-PowerCLIConfiguration Alias Answer-VMQuestion Set-VMQuestion
Table 1. PowerCLI 5.0.1 aliases.
You see there are only seven PowerCLI aliases. And these are not for convenience. They are so called “transitional aliases” for compatibility reasons with older PowerCLI versions. So the answer to my previous question is zero.
That means that if we want to use convenience aliases in PowerCLI, we have to make them our selves. So lets do that!
Creating aliases for PowerCLI cmdlets
You can use the New-Alias cmdlet to create a new alias. The command in listing 2 will a create an alias gvm for the Get-VM cmdlet.
New-Alias -Name gvm -Value Get-VM
Listing 2. New-Alias cmdlet to create the alias gvm for Get-VM.
If you want to make more aliases at once, you can use a hashtable to define the aliases.
$Aliases = @{ "cvis" = "Connect-VIServer" "gvmh" = "Get-VMHost" "gvm" = "Get-VM" } $Aliases.Keys | ForEach-Object {New-Alias -Name $_ -Value $Aliases[$_]}
Listing 3. PowerShell script to create aliases from a hashtable.
If you try this script after you have tried the New-Alias cmd from listing 2, you will get an error message:
New-Alias : Alias not allowed because an alias with the name ‘gvm’ already exists.
This message states that you are not allowed to change to alias. This is helpfull to see if we want to define an alias that already exists. If you still want to define the alias, and overwrite the former alias definition, you can use the New-Alias cmdlet with the -Force parameter.
New-Alias -Name gvm -Value Get-VM -Force
Listing 4. Use the New-Alias -Force parameter to overwrite an existing alias.
I want to create aliases for all the PowerCLI cmdlets. Lets first check how many PowerCLI cmdlets there are.
Get-VICommand | Measure-Object
Listing 5. Command to get the number of PowerCLI cmdlets.
If I run the command from listing 5 in PowerCLI 5.0.1, I find 352 cmdlets. That is a lot of cmdlets. If I have to define a new alias for all these cmdlets, that will keep me busy for some time. But I am more an automative than a creative guy. So lets try to automate this.
I want to create aliases that are all the capital letters of each cmdlet name. This will make the aliases easy to remember. If you know which letters are written in capitals in the PowerCLI cmdlet documentation, you will almost for sure know the alias. E.g. the alias for the Connect-VIServer cmdlet will become cvis. This method will generate some doubles, so we have to figure out which ones that are.
I ran the PowerCLI script from listing 6 in a new PowerLI session. It showed me all the double aliases and the corresponding cmdlet names. The output is in the form of a hash table. So I can edit this output and create a new hashtable with unique names.
# Create a character array with the hyphen and all the lowercase characters. $CharactersToRemove = "-abcdefghijklmnopqrstuvwxyz" $CharactersToRemoveArray = @() for ($i=0; $i -lt $CharactersToRemove.Length; $i++) { $CharactersToRemoveArray += $CharactersToRemove.Chars($i) } # Create aliases for all the PowerCLI cmdlets by removing the hyphen # and the lowercase characters from the cmdlet name. # Create a hashtable entry for the aliases that already exist. Get-VICommand | ForEach-Object { $Cmdlet = $_.Name $Alias = $Cmdlet $CharactersToRemoveArray | ForEach-Object { $Alias = $Alias.Replace("$_","") } $Alias = $Alias.ToLower() if (Get-Alias -Name $Alias -ErrorAction:SilentlyContinue) { Write-Output """$Alias"" = ""$Cmdlet""" } else { New-Alias -Name $Alias -Value $Cmdlet } }
Listing 6. Script to show the aliases that already exist.
I used the output of the script from listing 6 to make a hashtable with the aliases that already exist. I have editted this table to make the aliases unique. These unique aliases are used in listing 7 to generate aliases for all PowerCLI cmdlets.
# Create a character array with the hyphen and all the lowercase characters. $CharactersToRemove = "-abcdefghijklmnopqrstuvwxyz" $CharactersToRemoveArray = @() for ($i=0; $i -lt $CharactersToRemove.Length; $i++) { $CharactersToRemoveArray += $CharactersToRemove.Chars($i) } # Create aliases for all the PowerCLI cmdlets by removing the hyphen # and the lowercase characters from the cmdlet name. Get-VICommand | ForEach-Object { $Cmdlet = $_.Name $Alias = $Cmdlet $CharactersToRemoveArray | ForEach-Object { $Alias = $Alias.Replace("$_","") } $Alias = $Alias.ToLower() New-Alias -Name $Alias -Value $Cmdlet -ErrorAction:SilentlyContinue } # For some cmdlets the generated alias already exist. # The hashtable AliasesTable contains the handmade aliases for these cmdlets. $Aliases = @{ "adre" = "Apply-DrsRecommendation" "gcat" = "Get-Catalog" "gcl" = "Get-Cluster" "gco" = "Get-Compliance" "gda" = "Get-Datastore" "gder" = "Get-DeployRule" "gdre" = "Get-DrsRecommendation" "gdru" = "Get-DrsRule" "gin" = "Get-Inventory" "glo" = "Get-Log" "gme" = "Get-Media" "gpa" = "Get-Patch" "gpvd" = "Get-ProviderVdc" "gsta" = "Get-Stat" "gtem" = "Get-Template" "gvd" = "Get-Vds" "gvdvpspo" = "Get-VdsDVPortShapingPolicy" "gvi" = "Get-View" "gvipri" = "Get-VIPrivilege" "gvipro" = "Get-VIProperty" "gvmhat" = "Get-VMHostAttributes" "gvmhau" = "Get-VMHostAuthentication" "gvmhdpa" = "Get-VMHostDiskPartition" "gvmhpr" = "Get-VMHostProfile" "gvmhsn" = "Get-VMHostSnmp" "gvmhst" = "Get-VMHostStorage" "ivmhpa" = "Install-VMHostPatch" "mda" = "Move-Datacenter" "min" = "Move-Inventory" "mte" = "Move-Template" "nda" = "New-Datastore" "nder" = "New-DeployRule" "ndru" = "New-DrsRule" "nvd" = "New-Vds" "nvipr" = "New-VIProperty" "rin" = "Remediate-Inventory" "rda" = "Remove-Datacenter" "rds" = "Remove-Datastore" "rder" = "Remove-DeployRule" "rdru" = "Remove-DrsRule" "rfo" = "Remove-Folder" "rei" = "Remove-Inventory" "rvd" = "Remove-Vds" "rvipr" = "Remove-VIProperty" "revm" = "Restart-VM" "rvmho" = "Restart-VMHost" "sin" = "Scan-Inventory" "sclo" = "Search-Cloud" "scl" = "Set-Cluster" "sda" = "Set-Datastore" "sdru" = "Set-DrsRule" "svd" = "Set-Vds" "svdvpspo" = "Set-VdsDVPortShapingPolicy" "svmhau" = "Set-VMHostAuthentication" "svmhsn" = "Set-VMHostSnmp" "svmhst" = "Set-VMHostStorage" "spa" = "Stage-Patch" "svap" = "Start-VApp" "stvm" = "Start-VM" "svmho" = "Start-VMHost" "svmhse" = "Start-VMHostService" "sta" = "Stop-Task" "stva" = "Stop-VApp" "stovm" = "Stop-VM" "stvmh" = "Stop-VMHost" "stvmhs" = "Stop-VMHostService" "suvm" = "Suspend-VM" "svmgu" = "Suspend-VMGuest" "suvmh" = "Suspend-VMHost" "wto" = "Wait-Tools" } # Create the handmade aliases. $Aliases.Keys | ForEach-Object {New-Alias -Name $_ -Value $Aliases["$_"]}
Listing 7. Script to to define aliases for all the PowerCLI cmdlets.
Remember that if you save the script from listing 7 to a file, e.g. Set-PowerCLIAlias.ps1, and you run this script, you have to dot-source the script. Otherwise the aliases will be gone after the script has finished.
. .\Set-PowerCLIAlias.ps1
Listing 8. Dot sourcing a script will keep the aliases defined in the script after the script has finished.
Find PowerCLI cmdlets without an alias
We can use the script from listing 9 to see if there are PowerCLI cmdlets that are still missing an alias. If I run this script after running the script from listing 7, it doesn’t return anything. So all the PowerCLI cmdlets have an alias now.
$Alias = Get-Alias Get-VICommand | ForEach-Object { $VICommand = $_ if (-not ($Alias | Where-Object {$_.Definition -eq $VICommand.Name})) { $VICommand } }
Listing 9. Script to show the PowerCLI cmdlets without an alias.
There is another check we can do. We can count how many aliases there are for PowerCLI cmdlets. Listing 10 is a modification of the script from listing 1 that shows us this number.
$Alias = Get-Alias Get-VICommand | ForEach-Object { $VICommand = $_ $Alias | Where-Object {$_.Definition -eq $VICommand.Name} } | Measure-Object
Listing 10. Script to show the number of PowerCLI aliases.
If I run this script after I ran the script of listing 7, I count 359 PowerCLI aliases. PowerCLI out of the box has 7 aliases. There are 352 PowerCLI cmdlets for which we made a new aliases. That brings us a total of 359 aliases. Thus the script from listing 7 seems to work fine.
Update your PowerShell profile
If you want to have the PowerCLI aliases available in all your PowerCLI sessions, you can edit your PowerShell profile and put the script from listing 7 in your profile. You can do this by editing your profile with:
notepad $Profile
Listing 11. You can edit your profile by opening it in Notepad.
Warning
Use the aliases only if you are typing commands in the PowerCLI console.
Please use the full cmdlet names if you write a script. It will make the script a lot easier to read for you and someone else. If you use a script editor that knows about PowerShell, e.g. the PowerGUI Script Editor, it will have cmdlet name completion. So you don’t have to type the whole cmdlet names in the editor.
Pingback: PowerCLI 5.0.1 Connect-VIServer repaired « Robert van den Nieuwendijk
Cool script Robert! One thing I would suggest though would be to use common aliases for verbs when they are defined. For example, s is the alias for “set”, sa is the alias for “start”, and sp is the alias for “stop”. This allows for more intelligent guesswork. You can see the list of common aliases here:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd878329(v=vs.85).aspx
It doesn’t contain the entire list of verbs, but it’s a start. At some point I’m hoping that Microsoft defines common aliases for all verbs so that there is no ambiguity, but they haven’t fulfilled that request yet.
Thanks for your comment Kirk. And thanks for your suggestion and the link also. I had not seen that article before. But now I’m thinking about changing my script to reflect the article and your suggestion.