Vijay khurava
Vijay khurava

Reputation: 55

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 }

Upvotes: 0

Views: 3921

Answers (3)

DBADon
DBADon

Reputation: 579

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.

Upvotes: 0

lit
lit

Reputation: 16236

Using -Include on Get-ChildItem will allow you to specify a list of extensions. The -ErrorAction will cause it to skip drives that are not available such as an unmounted CD drive.

Get-PSDrive -PSProvider FileSystem |
    ForEach-Object  {
        Get-ChildItem -Path $_.Root -Recurse -Include '*.doc*', '*.txt' -ErrorAction SilentlyContinue |
            ForEach-Object { $_.Name }
    } |
        ForEach-Object {[PSCustomObject]@{HashCode = $_.GetHashCode(); FullName = $_.FullName}}
} |
Export-Csv -Path $TempFile -NoTypeInformation -Encoding ASCII

Update:

Here is a better way. It will prevent unknown extensions from getting into the mix such as "Microsoft.NET.Sdk.Publish.Docker.targets."

$ExtensionList = @('.txt', '.doc', '.docx', '.mp3')
$TempFile = Join-Path -path $Env:TEMP -ChildPath "$($pid.ToString()).tmp"

Get-PSDrive -PSProvider FileSystem |
    ForEach-Object  {
        Get-ChildItem -Path $_.Root -Recurse -ErrorAction SilentlyContinue |
            Where-Object { $ExtensionList -contains $_.Extension } |
        ForEach-Object {
            [PSCustomObject]@{
                HashCode = $_.GetHashCode();
                DirectoryName = $_.DirectoryName
                Name = $_.Name
            }
        }
    } |
    Export-Csv -Path $TempFile -Delimiter ';' -NoTypeInformation -Encoding ASCII
Write-Host "The temp file is $TempFile"

Upvotes: 1

Adam
Adam

Reputation: 4168

Checkout the Get-PSDrive cmdlet. It returns a list of drives, and you can specify just disk drives with the -PSProvider FileSystem parameter:

foreach ( $drive in $(Get-PSDrive -PSProvider FileSystem) ) {
Get-ChildItem -Path $drive.Root -Filter ***.doc** -Recurse -File |
Sort-Object Length -Descending | 
ForEach-Object { $_.BaseName }
}

Didn't test that but you get the idea.

Upvotes: 1

Related Questions