Powershell: List all File types in a directory tree

How do I list all file types in a directory tree?

This code gets all the files in a directory tree and then iterates through them collecting the file type extension. These extensions are added to an array which can then be output or processed.

<# List all file types in a Directory Tree#>

<# list all files in directory tree and store to object array #>
$DataFiles = Get-ChildItem -Path "$DirectoryPath" -Name -File -recurse

<# Create array list for file extensions to be stored #>
$FileExtensions = New-Object System.Collections.ArrayList

<# This for loop adds all file extensions to the file extensions array #>
foreach ($DataFile in $DataFiles) {
              <# get extension of current data file being processed #>
              $ext = [IO.Path]::GetExtension($DataFile)
              <# check if extension is already in array list #>
              $found = $FileExtensions.BinarySearch($ext)
              <# if the file extension is not in the array list then add it #>
              if($found -lt 0){
                             $FileExtensions.add($ext) > null

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s