Search code examples
windowspowershelldirectorysubdirectory

Powershell command to fetch all file path for all desired files extensions


I want to search all drives using PowerShell on windows machine to get the list of all files along with their extensions -

  1. Based on desired extension we pass in it like - *.mp3 or
  2. Fetch all files with multiple extensions like - *.txt, *.mp3 etc.

I tried below script but its giving only information from where we are running it. But I want to scan whole machine.

Get-ChildItem -Path .\ -Filter ***.doc** -Recurse -File| Sort-Object Length -Descending | ForEach-Object { $_.BaseName }

Solution

  • This is more than what the original question asked, but if you are going to go through the trouble of listing all your files, I suggest getting the filehash as well so you can determine if you have duplicates. A simple file name search will not detect if the same file has been saved with a different name. Adding to what @lit (https://stackoverflow.com/users/447901/lit) has posted:

    $ExtensionList = @('.txt', '.doc', '.docx', '.mp3')
    Get-PSDrive -PSProvider FileSystem |
        ForEach-Object  {
            Get-ChildItem -Path $_.Root -Recurse -ErrorAction SilentlyContinue |
                Where-Object { $ExtensionList -eq $_.Extension } |
                ## ForEach-Object { $_.Name, $_.FullName, $_.GetHashCode() }
                Select-Object @{Name="Name";Expression={$_.Name}}, @{Name="Hash";Expression={$_.GetHashCode()}}, @{Name="FullName";Expression={$_.FullName}} | 
                Export-Csv -Path C:\Temp\testing.csv -NoTypeInformation -Append
        }
    

    The addition of the file hash will allow you to see if you have duplicates and the full name will allow you to see where they are located.