You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

优化跨网络搜索9个目录的PowerShell脚本以提升运行速度

Optimizing Your Network Directory Search PowerShell Script

First off, let's fix that messy source path setup—having separate $source1, $source2, etc., is totally unnecessary and hard to maintain. Let's replace them with a single array, which will make your script way cleaner and easier to update:

$sources = @(
    "\\Server_IP\d$\Folder\Err",
    "\\Server_IP2\d$\Folder\Err",  # I assume your actual paths have distinct servers/folders
    "\\Server_IP3\d$\Folder\Err",
    # Add all 9 of your network paths here
)

Now, let's tackle the speed issue. Network file operations are inherently slower, but we can make several tweaks to cut down on lag:

1. Use Efficient Get-ChildItem Parameters

If you're using Get-ChildItem without optimizations, it's probably pulling more data than needed. Here are key flags to add:

  • -Filter: Way faster than -Include because it filters files on the remote server instead of downloading everything first. Use this if you're targeting specific file types (e.g., *.log).
  • -File/-Directory: Narrow down exactly what you're searching for to avoid extra processing.
  • -Recurse: Only use this if you need to dig into subdirectories—skip it if you just need the top-level folder.
  • -ErrorAction SilentlyContinue: Skips inaccessible paths without stopping the whole script.

Example optimized search:

foreach ($path in $sources) {
    Get-ChildItem -Path $path -Filter "*.log" -File -Recurse -ErrorAction SilentlyContinue
}

2. Parallel Processing with ForEach-Object -Parallel

Since you're hitting multiple network paths, running searches in parallel can drastically reduce total time (this works in PowerShell 7+). Here's how to implement it:

# Requires PowerShell 7 or newer
$sources | ForEach-Object -Parallel {
    Get-ChildItem -Path $_ -Filter "*.log" -File -Recurse -ErrorAction SilentlyContinue
} -ThrottleLimit 3  # Adjust based on your network capacity—start with 3-5 to avoid overload

The -ThrottleLimit controls how many parallel searches run at once. Tweak it based on how your network handles the load.

3. Avoid Unnecessary Properties

By default, Get-ChildItem returns a ton of file metadata. If you only need specific properties (like FullName, LastWriteTime), use -Select to limit the output—this cuts down on data transferred over the network:

Get-ChildItem -Path $path -Filter "*.log" -File -Recurse -ErrorAction SilentlyContinue | Select-Object FullName, LastWriteTime

4. Try Robocopy for Faster Enumeration

Robocopy is built specifically for network file operations and is often faster than PowerShell's cmdlets for large directory listings. You can call it from PowerShell to list files without copying anything:

foreach ($path in $sources) {
    # /L = list only (no copy), /S = include subdirectories, /NJH/NJS = skip headers/summaries
    robocopy $path . /L /S /NP /NC /NJH /NJS
}

You can parse the output if you need to process the files further. This is especially useful for deep directory structures over slow networks.

Quick Recap of Key Fixes

  • Ditch individual $sourceN variables for a single array to clean up your code.
  • Use Get-ChildItem with -Filter, -File/-Directory, and -ErrorAction to reduce overhead.
  • Use parallel processing (PowerShell 7+) to run multiple searches at once.
  • Limit the properties you retrieve to cut down network data transfer.
  • Give Robocopy a try if PowerShell's cmdlets are still too slow.

If you can share more details about what exactly you're searching for (specific file types, patterns, or actions you need to take on the files), I can refine this even further!

内容的提问来源于stack exchange,提问作者user9687423

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:09:51