DIRing folders in PowerShell
Now that in Powershell dir
is just an alias to Get-ChildItem
, how can I get a list of just folders?
I've been doing dir /ad
(for Attribute Directory) in the command prompt for about 20 years. Is there any way to alias this with parameters in PowerShell?
I see How do I get only directories using Get-ChildItem? over on Stack Overflow but I'm not going to type Get-ChildItem -Recurse | ?{ $_.PSIsContainer }
by hand every time. Ideally I'd like dir /ad
to alias to that command.
Put a function like this in your profile:
function d([string]$switch)
{
if ($switch -eq "d")
{
Get-ChildItem -Recurse | ?{ $_.PSIsContainer }
}
elseif ($switch -eq "f")
{
Get-ChildItem -Recurse | ?{ !$_.PSIsContainer }
}
else
{
Get-ChildItem -Recurse
}
}
then just use
d d
You cannot use parameters on aliases, but functions work just the same way. You get use d -d
rather than just d d
, or you could make directory the default and use just d
, the possibilities are endless. You could also pass in the path.
how can I get a list of just folders?
gci -d
is only seven keystrokes counting Enter... and is there waiting for you, if you're willing to upgrade your Powershell to v3 or better. :)
gci -d -r
if you want recursion.
Edit: Removed the tab keystrokes because they are not necessary.