如何在PowerShell中将两个不同路径合并到同一个对象中
PowerShell 多源路径拼接调整方案
你当前写法的问题是Join-Path的-ChildPath参数不支持一次性传入多个路径值批量拼接,无法直接生成包含两个完整目标路径的数组。
调整代码
你可以通过遍历子路径分别拼接的方式实现需求,兼容所有PowerShell版本:
$RetailSource = '\Retail P&C Sales Intelligence\*' $GroupSource = '\Group P&C Sales Intelligence\*' $sourcepath = $RetailSource, $GroupSource | ForEach-Object { Join-Path -Path 'c:\uploadtool' -ChildPath $_ }
如果不需要处理跨平台路径分隔符兼容,也可以用更简单的字符串拼接写法:
$basePath = 'c:\uploadtool' $RetailSource = '\Retail P&C Sales Intelligence\*' $GroupSource = '\Group P&C Sales Intelligence\*' $sourcepath = "$basePath$RetailSource", "$basePath$GroupSource"
后续使用说明
生成的$sourcepath是包含两个完整路径的数组,可直接传入Get-ChildItem使用:
Get-ChildItem -Path $sourcepath
注意原代码中的&是HTML转义字符,实际编写代码时直接使用&即可。
内容的提问来源于stack exchange,提问作者essex lake
相关产品推荐
相关产品推荐

