使用更好的PowerShell脚本将文件应用于多个文件夹
我正在从事一个项目,必须经常将文件应用于多个文件夹。我正在尝试学习一些PowerShell命令,以使其更轻松一些。我想到了以下脚本,但是我觉得这太冗长了,可以用一个更好的脚本进行蒸馏:
[string]$sourceDirectory = "C:\Setup\App Folder Files\*"
# Create an array of folders
$destinationDirectories = @(
'C:\Users\GG_RCB1\Documents\',
'C:\Users\GG_RCB2\Documents\',
'C:\Users\LA_RCB1\Documents\',
'C:\Users\PR_RCB1\Documents\',
'C:\Users\PQ_RCB1\Documents\',
'C:\Users\PQ_RCB2\Documents\',
'C:\Users\XC_RCB1\Documents\',
'C:\Users\XC_RCB2\Documents\',
'C:\Users\XC_RCB3\Documents\',
'C:\Users\XC_RCB4\Documents\',
'C:\Users\XC_RCB5\Documents\',
'C:\Users\XC_RCB6\Documents\',
'C:\Users\XC_RCB7\Documents\',
'C:\Users\XC_RCB8\Documents\')
# Perform iteration to create the same file in each folder
foreach ($i in $destinationDirectories) {
Copy-item -Force -Recurse -Verbose $sourceDirectory -Destination $i
}
我进入此过程,知道用户文件夹区域中的每个文件夹都将具有相同的格式:_rcb<#> \ documents \
我知道我可以使用此代码循环浏览这些文件:
Get-ChildItem -Path 'C:\Users'| where-object {$_.Name -match "^[A-Z][A-Z]_RCB"}
我不确定该怎么做是在该循环中如何钻入文档文件夹并执行副本。我想避免必须继续更新第一个代码示例的数组,尤其是当我知道用户文件夹中的子文件夹的命名约定时。我只是在寻找一种更干净的方法来做到这一点。
感谢您的建议!
I'm working on a project where I have to apply a file to multiple folders every so often. I'm trying to learn some PowerShell commands to make this a little easier. I came up with the following script, which works, but I feel that this is too verbose and could be distilled down with a better script:
[string]$sourceDirectory = "C:\Setup\App Folder Files\*"
# Create an array of folders
$destinationDirectories = @(
'C:\Users\GG_RCB1\Documents\',
'C:\Users\GG_RCB2\Documents\',
'C:\Users\LA_RCB1\Documents\',
'C:\Users\PR_RCB1\Documents\',
'C:\Users\PQ_RCB1\Documents\',
'C:\Users\PQ_RCB2\Documents\',
'C:\Users\XC_RCB1\Documents\',
'C:\Users\XC_RCB2\Documents\',
'C:\Users\XC_RCB3\Documents\',
'C:\Users\XC_RCB4\Documents\',
'C:\Users\XC_RCB5\Documents\',
'C:\Users\XC_RCB6\Documents\',
'C:\Users\XC_RCB7\Documents\',
'C:\Users\XC_RCB8\Documents\')
# Perform iteration to create the same file in each folder
foreach ($i in $destinationDirectories) {
Copy-item -Force -Recurse -Verbose $sourceDirectory -Destination $i
}
I go into this process knowing that every folder in the User folder area is going to have the same format: _RCB<#>\Documents\
I know that I can loop through those files using this code:
Get-ChildItem -Path 'C:\Users'| where-object {$_.Name -match "^[A-Z][A-Z]_RCB"}
What I'm not sure how to do is to how, within that loop, drill down to the Documents folder and do the copy. I want to avoid having to keep updating the array from the first code sample, particularly when I know the naming convention of the subfolders in the Users folder. I'm just looking for a cleaner way to do this.
Thanks for any suggestions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,我会继续发布我的想法。不要从 @mathias 建议中提出建议,但要提供我的解决方案,这是我的看法:
由于每个人都喜欢可以满足您需求的“ 单线”。
Get-ChildItem
在其路径中接受通配符,这使我们一口气完成了这一点。鉴于您的目录是...幸运的是,
copy-item
也具有一些很酷的功能,例如能够使用脚本块,该脚本将允许通过$_。FullName
属性是目标,而它们一次被传递给管道。当您决定结果是您之后的结果时,请删除
Whatif
公共参数。Ehh, I'll go ahead and post what I had in mind as well. Not to take away from @Mathias suggestion in the comments, but to offer my solution, here's my take:
Since everyone loves the "One-Liners" that can accomplish your needs.
Get-ChildItem
accepts wildcard-expressions in it's path which let's us accomplish this in one go. Given that your directories are...Luckily,
Copy-Item
also has some cool features on it's own such as being able to use a script block that will allow the passing of$_.FullName
property as it's destination, while they are passed down the pipeline one at a time.Remove the
-WhatIf
common parameter when you've dictated the results are what you're after.