PowerTip: Use a PowerShell 3.0 Switch to Find Only Directories

PowerTip: Use a PowerShell 3.0 Switch to Find Only Directories

  • Comments 4
  • Likes

Summary: Use a switch in Windows PowerShell 3.0 to easily find only directories.

Hey, Scripting Guy! Question How can I use Windows PowerShell 3.0 to find a list of only directories from my current location?

 Hey, Scripting Guy! Answer Use the –Directory switch with the Get-ChildItem cmdlet.

Get-ChildItem -Directory

Note  The –Directory switch was added to Windows PowerShell 3.0.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • PoSh v2 show only directories:

    gci | ?{$_.psiscontainer}

    PoSh v2 show only files:

    gci | ?{$_directory}

  • Sorry. i keep messing up. forgot the dot

    gci | ?{$_.directory}

  • I've been looking for a variation of this that returns a specified directory and its children, not just the children. Is there something that will do that? I can describe my need, if it will help.

  • Not everyone likes prompts, but it makes scripts more generic.

    do {

     $toplevel = Read-Host -Prompt "Enter Directory"

    } until (Test-Path $toplevel)

    $arrDirs = @($toplevel)

    Get-ChildItem $toplevel -Recurse -Directory | ForEach-Object { $arrDirs += $_.FullName}

    $arrDirs