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

如何在PowerShell的Get-ChildItem中排除node_modules避免长路径错误

解决PowerShell查找.csproj时跳过node_modules目录的问题

为什么你之前的-Exclude写法无效

Get-ChildItem的-Exclude参数仅对当前遍历层级的文件名/目录名生效,不支持递归通配符(比如**\)。你尝试的几种写法都无法让PowerShell跳过深层的node_modules目录,因此还是会进入该目录触发长路径错误。

可行的解决方法

方法1:先排除目录再查找文件

先递归筛选出所有非node_modules的目录,再在这些目录里查找.csproj文件,从根源上跳过node_modules:

Get-ChildItem -Path .\ -Recurse -Directory -Exclude node_modules | Get-ChildItem -Filter *.csproj

方法2:用Where-Object过滤路径(适合临时忽略错误场景)

如果不想调整目录遍历逻辑,可以先屏蔽长路径错误,再过滤掉所有来自node_modules的结果:

Get-ChildItem -Path .\ -Recurse -File -Filter *.csproj -ErrorAction SilentlyContinue | 
    Where-Object { $_.FullName -notmatch '\\node_modules\\' }

注:-ErrorAction SilentlyContinue会屏蔽所有错误,若其他目录存在异常也会被忽略,需谨慎使用。

方法3:启用系统长路径支持(彻底解决长路径问题)

如果经常需要处理长路径文件,可通过修改系统注册表启用长路径支持(需管理员权限):

# 启用系统级长路径支持(重启后仍有效)
Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem' -Name 'LongPathsEnabled' -Value 1 -Type DWord
# 之后执行查找命令即可正常处理长路径
Get-ChildItem -Path .\ -Recurse -Filter *.csproj

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 16:32:44