How to speed up Powershell Get-Childitem over UNC

前端 未结 4 2061
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 10:33

DIR or GCI is slow in Powershell, but fast in CMD. Is there any way to speed this up?

In CMD.exe, after a sub-second delay, this responds a

4条回答
  •  渐次进展
    2020-11-28 11:19

    Here is a good explanation on why Get-ChildItem is slow by Lee Holmes. If you take note of the comment from "Anon 11 Mar 2010 11:11 AM" at the bottom of the page his solution might work for you.

    Anon's Code:

    # SCOPE: SEARCH A DIRECTORY FOR FILES (W/WILDCARDS IF NECESSARY)
    # Usage:
    # $directory = "\\SERVER\SHARE"
    # $searchterms = "filname[*].ext"
    # PS> $Results = Search $directory $searchterms
    
    [reflection.assembly]::loadwithpartialname("Microsoft.VisualBasic") | Out-Null
    
    Function Search {
      # Parameters $Path and $SearchString
      param ([Parameter(Mandatory=$true, ValueFromPipeline = $true)][string]$Path,
      [Parameter(Mandatory=$true)][string]$SearchString
      )
      try {
        #.NET FindInFiles Method to Look for file
        # BENEFITS : Possibly running as background job (haven't looked into it yet)
    
        [Microsoft.VisualBasic.FileIO.FileSystem]::GetFiles(
        $Path,
        [Microsoft.VisualBasic.FileIO.SearchOption]::SearchAllSubDirectories,
        $SearchString
        )
      } catch { $_ }
    
    }
    

提交回复
热议问题