【问题标题】:how to copy a file to all desktop folders in the C: Users directory using powershell script如何使用powershell脚本将文件复制到C:用户目录中的所有桌面文件夹
【发布时间】:2021-01-02 15:36:32
【问题描述】:

我正在尝试使用 powershell 脚本将文件复制到 C:Users 目录中的所有桌面文件夹。如果将文件夹名称放在脚本中,我可以对单个文件夹执行此操作。使用通配符时,我收到非法字符错误,它不起作用。脚本如下;有什么建议吗?

Copy-Item -Path "\\Server IP\C$\Users\Default\Desktop\file.url" -Destination "\\Server IP\c$\Users\*\Desktop" -Force

【问题讨论】:

  • 您提供的脚本中没有通配符。
  • 尝试评估引号中的部分>>> -Destination "\Server IP\c$\Users*\Desktop"。您会注意到它的通配符直接附加到Users ...,而这不是用户配置文件目录所在的位置。 [咧嘴]

标签: powershell copy-item


【解决方案1】:

如果你把这个命令放在这样的 foreach 循环中,你就可以做到:

Get-ChildItem -Path "\Server IP\c$\Users" -Directory | foreach {

    Copy-Item -Path "\Server IP\C$\Users\Default\Desktop\file.url" -Destination "\Server IP\c$\Users\$_.Name\Desktop" -Force

}

【讨论】:

  • UNC 路径应以双反斜杠开头。此外,您还想排除目的地\\Server IP\c$\Users\Public
猜你喜欢
  • 2015-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多