Friday, December 20, 2024
HomeWindowsHow to find and use an alias or function for cmdlets and...

How to find and use an alias or function for cmdlets and commands in Window PowerShell

Windows PowerShell contains many powerful commands and cmdlets, but sometimes typing the full name for commonly used commands is a pain. Good news! Many of the built-in commands already have shortcuts, a.k.a. Aliases.

To see the current list of aliases, simply type the following in PowerShell:

Get-Alias

You will be presented with a list of aliases: commands on the right, alias, or shortcut, on the left. Let’s see it again with its own alias:

gal

To create your own Aliases, you use the Set-Alias command. See here:

help Set-Alias

Note that aliases which you set are not going to be available once you leave PowerShell, by default. To make your “user aliases” available the next time that you open PowerShell, you will need to either export and then import them, or put them in your PowerShell Profile file.

You probably don’t have a PowerShell Profile file yet. Find out like so:

test-path $profile

This returns True if it exists, False if it does not. Create a new file with the following command:

ni -path $profile -itemtype file -force

Now you can simply put any aliases (actually, the command that sets the alias) which you wish to have available every time you launch PowerShell in that file. You can do this by echoing your set-alias command in to the file, or by opening the file with, say, Notepad, and typing in the command manually.

For example, let us create our own alias for the get-childitem command:

set-alias l Get-ChildItem

Now you can simply type l (that’s lowercase “L”) to run the Get-ChildItem command. Try it out with something like the following:

l c:\

To make this alias “persistent”, simply run the following:

echo “set-alias l Get-ChildItem” >> $Profile

One thing about aliases is that they do not allow you to specify command options, for example the “-recurse” option for the get-childitem command. For that, we need to create a function.

To create a function that lets us run “Get-ChildItem -recurse” with the shorter command “ll” (lowercase “LL”), type the following:

echo “function ll {get-childitem `$args -recurse}” >> $Profile

Now close and re-open PowerShell, and run:

ll $env:windir\system32\logfiles

What is this actually doing? It’s creating a function that can be called by its name, ll, and that function is running a command, get-childitem, passing to it any arguments, $args, which happen to be passed to the function when it’s called, and then specifying the -recurse option for the get-childitem command. In our command above we’re passing “$env:windir\system32\logfiles” to our ll function, which means that on most Windows installations we’ll see all of the contents of c:\windows\system32\logfiles, including all subdirectories.


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!