Get full path of the files in PowerShell

后端 未结 14 2113
挽巷
挽巷 2020-12-02 06:21

I need to get all the files including the files present in the subfolders that belong to a particular type.

I am doing something like this, using Get-ChildItem:

相关标签:
14条回答
  • 2020-12-02 07:15

    This should perform much faster than using late filtering:

    Get-ChildItem C:\WINDOWS\System32 -Filter *.txt -Recurse | % { $_.FullName }
    
    0 讨论(0)
  • 2020-12-02 07:16

    You can also use Select-Object like so:

    Get-ChildItem "C:\WINDOWS\System32" *.txt -Recurse | Select-Object FullName
    
    0 讨论(0)
  • 2020-12-02 07:20

    Add | select FullName to the end of your line above. If you need to actually do something with that afterwards, you might have to pipe it into a foreach loop, like so:

    get-childitem "C:\windows\System32" -recurse | where {$_.extension -eq ".txt"} | % {
         Write-Host $_.FullName
    }
    
    0 讨论(0)
  • 2020-12-02 07:22

    Really annoying thing in PS 5, where $_ won't be the full path within foreach. These are the string versions of FileInfo and DirectoryInfo objects. For some reason a wildcard in the path fixes it, or use Powershell 6 or 7. You can also pipe to get-item in the middle.

    Get-ChildItem -path C:\WINDOWS\System32\*.txt -Recurse | foreach { "$_" }
    
    Get-ChildItem -path C:\WINDOWS\System32 -Recurse | get-item | foreach { "$_" }
    

    This seems to have been an issue with .Net that got resolved in .Net Core (Powershell 7): Stringification behavior of FileInfo / Directory instances has changed since v6.0.2 #7132

    0 讨论(0)
  • 2020-12-02 07:22
    gci "C:\WINDOWS\System32" -r -include .txt | select fullname
    
    0 讨论(0)
  • 2020-12-02 07:25

    Try this:

    Get-ChildItem C:\windows\System32 -Include *.txt -Recurse | select -ExpandProperty FullName
    
    0 讨论(0)
提交回复
热议问题