Prof. Powershell

PowerShell AKA

DOS commands seem to work in PowerShell, but not all is as it seems. Here's the trick.

You probably didn't realize it, but you already know some PowerShell commands even if you've never opened a PowerShell prompt. I'll prove it. Open a PowerShell prompt and type DIR:

PS C:\Users\Jeff> DIR

What did you get? A directory listing. Now, change to a directory:

PS C:\Users\Jeff> cd c:\temp
PS C:\Temp>

List the contents of a text file:

PS C:\Temp> type file.txt

Many of the commands you've come to know and love continue to work in PowerShell. That's because PowerShell includes an alias feature. DIR, CD, and TYPE are aliases to PowerShell cmdlets. Instead of having to learn the cmdlet that gives a directory listing, you can use the DIR command like you always have. However, this is not the DIR command from the CMD shell. At a prompt type help dir and you'll see the help information for Get-Childitem. Thus, when you run DIR, PowerShell is actually running Get-ChildItem.

This is why DIR /S won't work in PowerShell, because Get-ChildItem doesn't have a /S parameter. You would need to use -recurse.

To find all the installed aliases on your system you can browse the Alias PSDrive provider:

PS C:\> dir alias:

Or use the Get-Alias cmdlet. You can use it to identify a single alias and its corresponding cmdlet:

PS C:\> get-alias dir

CommandType Name Definition
----------- ---- ----------
Alias       dir  Get-ChildItem

Or if you type the command without any parameters, you'll see a list of all aliases.
The purpose of an alias is to save typing. Instead of having to type Get-WMIObject, you can simply type gwmi.

Curious about what aliases are defined for a given cmdlet? You could sort through the Get-Alias output, or use a command like this:

PS C:\> gal | where {$_.resolvedcommand -like "get-childitem"}

CommandType Name Definition
----------- ---- ----------
Alias       gci  Get-ChildItem
Alias       ls   Get-ChildItem
Alias       dir  Get-ChildItem

You can substitute the name of any cmdlet.

To create your own alias for a cmdlet, function or scriptblock use the New-Alias cmdlet:

PS C:\> new-alias d Get-Childitem
PS C:\> get-alias d

CommandType Name Definition
----------- ---- ----------
Alias       d    Get-Childitem

Any alias you create will be destroyed when your PowerShell session ends. If you need them to be persistent, add the command to create them in your profile.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.