Search code examples
powershelldirectory-tree

powershell how to display only the names of directories (except one) and subdirectories?


I want to display the directories and subdirectories in PowerShell (therefore in recursive mode) by excluding a directory located at the root. I tried with the following script: The exclusion works well because (note: I tried with "Exclude," but it did not work.) But my main problem is that it displays all files! In fact, I would only like to display the names of directories and subdirectories. Thanks a lot.

Get-ChildItem -Path "C:\" -include "*.*" -Recurse |  
  ?{$_.fullname -notlike "C:\Pictures\*"} | 
  %{ADD-content -path "C:\result.txt" -value $_.FullName}

 

Solution

  • To see the options for Get-childItem you can call Get-Help -full Get-ChildItem or just call help -full GCI. There you will find the option -Attributes which os what you need:

    Get-ChildItem -Attributes Directory -Path "C:\" -include "*.*" -Recurse |  
      ?{$_.fullname -notlike "C:\Pictures\*"} | 
      %{ADD-content -path "C:\result.txt" -value $_.FullName}