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

偶尔善良 提交于 2020-12-07 07:05:29

问题


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 }

回答1:


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.




回答2:


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.




回答3:


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"


来源:https://stackoverflow.com/questions/55794098/powershell-command-to-fetch-all-file-path-for-all-desired-files-extensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!