Saturday, 13 June 2015

PowerShell - List only Files or Folders by Recursively

We can retrieve only list of Files or Folders by Recursively using the Powershell cmdlet Get-ChildItem.

List Only Files:

Use the following script to get only list of Files from a Folder and its Sub Folder by using Recursive parameter.
Get-ChildItem -Recurse "C:\TestDir" | Where { ! $_.PSIsContainer } | Select Name,FullName,Length

List Only Folders:

Use the following PowerShell script to get only list of Folders from a Folder and its Sub Folder by using Recursive parameter.
Get-ChildItem -Recurse "C:\TestDir" | Where { $_.PSIsContainer } | Select Name,FullName

List Files and Exclude Some Files:

The following PowerShell script list all the files under the folder "C:\TestDir" but exclude the csv files.
Get-ChildItem -Recurse "C:\TestDir" -Exclude *.csv | Where {! $_.PSIsContainer } | Select Name,FullName

List Files and Exclude Files from a Folder:

The following powershell script list all the files under the folder "C:\TestDir" but exclude files from the Folder Test2.
Get-ChildItem -Recurse "C:\TestDir" |  Where-Object { $_.FullName -notmatch '\\Test2($|\\)' } | Where {! $_.PSIsContainer } | Select Name,FullName
The expression '\\Test2($|\\)' allow us to exclude the directory Test2 and its files.

Advertisements
Advertisements

No comments:

Post a Comment