#Using Set-Alias In PowerShell To Create Shortcuts To Functions In Windows 10
#These commands also work in both PowerShell and PowerShell Direct. You can also do this in Microsoft Windows Server 2019.
#Just the code:
|
1 2 3 4 |
Function funk_cnkermit {Enter-PSSession -ComputerName KERMIT -Credential DOMAIN\administrator} #Set fucntion to enter a PowerShell Remote session Set-Alias kermit funk_cnkermit #Create Alias using computer name Function funk_vmserver2019 {Enter-PSSession -VMName Server2019 -Credential DOMAIN\administrator} #Set function to enter a PowerShell Direct session Set-Alias server2019 funk_vmserver #Create Alias using virtual machine name |
#The Longer Story…
#These are two basic functions, but the functions you control with Set-Alias can be much more complicated. An Alias is just a shortcut to a command with a long name, but without any parameters. Parameters still need to be added to most commands when running an Alias. A function can contain commonly used parameters or even more complex command structures. You will also need to add permanent functions to your $Profile for PowerShell and PowerShell ISE separately since they are controlled by two different startup files.
#Using Set-Alias In PowerShell To Create Shortcuts To Functions
#As an example, I am going to create a function to run Enter-PSSession to connect using PowerShell Remote. I also create a similar function to connect using PowerShell Direct so I just type the value of either the -VMName or the -ComputerName to enter my password and connect.
#KERMIT is the computer name of my server. The name of the virtual machine is Server2019. These commands would need to be modified to fit your environment.
#List available Functions using Out-GridView in PowerShell ISE or not
#Note: Out-GridView does not work in a remote session or in PowerShell command prompt.
|
1 2 |
Get-ChildItem Function: #Take a look a the functions currently defined Get-ChildItem Function:|Out-GridView #Use PowerShell ISE to provide a grid view of the output |
#Check the pre-defined list of aliases using Out-GridView in PowerShell ISE or not
#See pre-defined Alias list
|
1 2 |
Get-ChildItem Alias: #Take a look at the currently defined alias list Get-ChildItem Alias:|Out-GridView #Use PowerShell ISE to provide a grid view of the output |
#Create Two New Functions To Connect To Server Using PowerShell Remote and PowerShell Direct
#One function called funk_cnkermit initiates a connection to the computer name (-ComputerName) of the server and the other, funk_vmserver2019, connects to a virtual machine name (-VMName). The function name can be anything that is guaranteed to stay unique. The command inside the {} could be any commands that you find useful and use frequently. Multiple lines of code are also supported.
|
1 2 |
Function funk_cnkermit {Enter-PSSession -ComputerName KERMIT -Credential DOMAIN\administrator} #Set fucntion to enter a PowerShell Remote session |
|
1 |
Function funk_vmserver2019 {Enter-PSSession -VMName Server2019 -Credential DOMAIN\administrator} #Set function to enter a PowerShell Direct session |
#Use The Set-Alias PowerShell Command To Create Shorter Commands And To Trigger Custom Functions Into Action Without Repetitive Keystrokes
#Set an alias to each new function using the computer name kermit and the virtual machine name server2019 using the commands below:
|
1 |
Set-Alias kermit funk_cnkermit #Create Alias using computer name |
|
1 |
Set-Alias server2019 funk_vmserver2019 #Create Alias using virtual machine name |
#Here Is A Useful Docker Function I Created
|
1 2 |
Function funk_DockerCleanExitedContainers {docker.exe rm $(docker ps -q -f status=exited)} #Function to automatically clean exited containers in Docker Set-Alias Docker-CleanExitedContainers funk_DockerCleanExitedContainers #Set Alias name |
#I’m sure there are much more complicated functions that I’ll add along the way, but just having an alias to Enter-PSSession is such a time saver. Don’t forget to use AutoComplete or Tab to save time when typing all of Docker-CleanExitedContainers. Once the Alias is set, it will be as if it was a command in the environment path.











































should appear on your Desktop after using F5 to refresh or on log off /reboot. Use the commands above to right-click and launch an elevated PowerShell prompt starting with the selected folder as a starting point.





