繁体   English   中英

编写 PowerShell 脚本以将目录及其内容复制到所有用户的文档文件夹

[英]Writing a PowerShell script to copy directory and its contents to all users documents folder

首先,这是我关于堆栈溢出的第一个真正的问题,所以我们在这里 go:

我正在尝试使用 Get-ChildItem 和 Copy-Item 将目录及其内容复制到可能位于 PC 上的每个用户配置文件,而不知道有多少用户或他们的用户名。 但是,每当我运行脚本或我认为实现结果的方式相同的其他变体时,我都会遇到非法字符问题。 我认为这是因为通配符,但我不知道我应该如何规避它。

例如:测试通配符:

Get-ChildItem 'C:Users\*\'

我可以看到所有用户和来自 Microsoft 的 Power-shell 文档使用 * 应该允许它包含该文件夹中的所有项目。 但是每当我将路径扩展到让我们说:

Copy-Item 'D:\Custom Office Templates' -Recurse -Destination 'C:\Users\*\Documents\' -Force

我收到“路径错误中的非法字符”。 如果我用实际用户名替换 * 以更正路径,我会得到我想要的单个用户。 尝试使用时会发生同样的事情。

ForEach-Object {Copy-Item -Path 'D:\Custom Office Templates' -Destination 'C:\users\*\Documents\Custom Office Templates' -Force -Recurse}

感谢您提供的任何帮助:)。

-Destination被读取为文字路径,不理解通配符。 您可以做的是将您已经拥有的内容与Get-ChildItem结合使用-Path上的通配符和-Destination上的延迟绑定脚本块

$source = 'D:\Custom Office Templates'
Get-ChildItem 'C:Users\*\' -Directory |
    Copy-Item -LiteralPath $source -Recurse -Destination {
        Join-Path $_.FullName 'Documents\Custom Office Templates'
    } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM