Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a PowerShell function to get directories?

Using PowerShell I can get the directories with the following command:

Get-ChildItem -Path $path -Include "obj" -Recurse | `
    Where-Object { $_.PSIsContainer }

I would prefer to write a function so the command is more readable. For example:

Get-Directories -Path "Projects" -Include "obj" -Recurse

And the following function does exactly that except for handling -Recurse elegantly:

Function Get-Directories([string] $path, [string] $include, [boolean] $recurse)
{
    if ($recurse)
    {
        Get-ChildItem -Path $path -Include $include -Recurse | `
            Where-Object { $_.PSIsContainer }
    }
    else
    {
        Get-ChildItem -Path $path -Include $include | `
            Where-Object { $_.PSIsContainer }
    }
}

How can I remove the if statement from my Get-Directories function or is this a better way to do it?

like image 710
Tim Murphy Avatar asked Jul 17 '10 05:07

Tim Murphy


People also ask

How do I get a list of directories in PowerShell?

-Directory To get a list of directories, use the Directory parameter or the Attributes parameter with the Directory property. You can use the Recurse parameter with Directory.

What PowerShell command is used to get the content of a folder?

The Complete PowerShell 7 course: Beginner To Advanced To display the directory content, Get-ChildItem cmdlet is used. You need to provide the path of the directory or if you are in the same directory, you need to use only Get-ChildItem directly.

How do I view a directory in PowerShell?

The Test-Path Cmdlet $Folder = 'C:\Windows' "Test to see if folder [$Folder] exists" if (Test-Path -Path $Folder) { "Path exists!" } else { "Path doesn't exist." } This is similar to the -d $filepath operator for IF statements in Bash. True is returned if $filepath exists, otherwise False is returned.


3 Answers

Try this:

# nouns should be singular unless results are guaranteed to be plural.
# arguments have been changed to match cmdlet parameter types
Function Get-Directory([string[]]$path, [string[]]$include, [switch]$recurse) 
{ 
    Get-ChildItem -Path $path -Include $include -Recurse:$recurse | `
         Where-Object { $_.PSIsContainer } 
} 

This works because -Recurse:$false is the same has not having -Recurse at all.

like image 159
x0n Avatar answered Sep 18 '22 09:09

x0n


In PowerShell 3.0, it is baked in with -File -Directory switches:

dir -Directory #List only directories
dir -File #List only files
like image 42
iraSenthil Avatar answered Sep 20 '22 09:09

iraSenthil


The answer Oisin gives is spot on. I just wanted to add that this is skirting close to wanting to be a proxy function. If you have the PowerShell Community Extensions 2.0 installed, you already have this proxy function. You have to enable it (it is disabled by default). Just edit the Pscx.UserPreferences.ps1 file and change this line so it is set to $true as shown below:

GetChildItem = $true # Adds ContainerOnly and LeafOnly parameters 
                     # but doesn't handle dynamic params yet.

Note the limitation regarding dynamic parameters. Now when you import PSCX do it like so:

Import-Module Pscx -Arg [path to Pscx.UserPreferences.ps1]

Now you can do this:

Get-ChildItem . -r Bin -ContainerOnly
like image 40
Keith Hill Avatar answered Sep 19 '22 09:09

Keith Hill