Powershell命令获取所有所需文件扩展名的所有文件路径 [英] Powershell command to fetch all file path for all desired files extensions

查看:112
本文介绍了Powershell命令获取所有所需文件扩展名的所有文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Windows计算机上使用PowerShell搜索所有驱动器,以获取所有文件及其扩展名的列表-

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

  1. 基于所需的扩展名,我们像- *.mp3
  2. 获取所有具有多个扩展名的文件,例如- *.txt,*.mp3 等.

我尝试了以下脚本,但是它仅提供运行位置的信息.但是我想扫描整个机器.

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 }

推荐答案

这比原始问题要多,但是如果您要列出所有文件的麻烦,建议您也获取文件哈希这样您就可以确定是否有重复项.简单的文件名搜索将不会检测是否已使用其他名称保存了相同的文件.添加到@lit( https://stackoverflow.com/users/447901/lit )发布的内容:

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.

这篇关于Powershell命令获取所有所需文件扩展名的所有文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆