powershell - Powershell复制项目问题

标签 powershell copy-item

我试图将用户的个人资料从另一个驱动器复制到C:驱动器。我已经失望了,但遇到了两个问题,我一直不停地努力,但对我来说却无济于事。

$user="JohnDoe"

Copy-Item -Path "H:\$user\Contacts" -Destination C:\Users\user\Desktop\UserProfile\$user -recurse -Force
Copy-Item -Path "H:\$user\Desktop" -Destination C:\Users\user\Desktop\UserProfile\$user -recurse -Force 
Copy-Item -Path "H:\$user\Documents" -Destination C:\Users\user\Desktop\UserProfile\$user -recurse -Force 
Copy-Item -Path "H:\$user\Downloads" -Destination C:\Users\user\Desktop\UserProfile\$user -recurse -Force 
Copy-Item -Path "H:\$user\Favorites" -Destination C:\Users\user\Desktop\UserProfile\$user -recurse -Force 
Copy-Item -Path "H:\$user\Links" -Destination C:\Users\user\Desktop\UserProfile\$user -recurse -Force 
Copy-Item -Path "H:\$user\Music" -Destination C:\Users\user\Desktop\UserProfile\$user -recurse -Force 
Copy-Item -Path "H:\$user\Pictures" -Destination C:\Users\user\Desktop\UserProfile\$user -recurse -Force 
Copy-Item -Path "H:\$user\Saved Games" -Destination C:\Users\user\Desktop\UserProfile\$user -recurse -Force 
Copy-Item -Path "H:\$user\Searches" -Destination C:\Users\user\Desktop\UserProfile\$user -recurse -Force 
Copy-Item -Path "H:\$user\Start Menu" -Destination C:\Users\user\Desktop\UserProfile\$user -recurse -Force 
Copy-Item -Path "H:\$user\Videos" -Destination C:\Users\user\Desktop\UserProfile\$user -recurse -Force 

当我运行代码时,发生的第一个问题是首先复制的所有文件夹,其所有内容都将放在
 C:\Users\user\Desktop\UserProfile\$user

例如,在我的代码中,原始H:驱动器“联系人”文件夹中的所有内容都将复制到C:Drive,而不是“联系人”文件夹中,而是路径位置
 C:\Users\user\Desktop\UserProfile\$user

第二个问题是在所有文件夹$ RECYCLE.BIN中创建了。



一些帮助将不胜感激。

最佳答案

在这里,我使用used a feature called Splatting将参数的哈希表应用于该函数:

$user = 'JohnDoe'

$ciArgs = @{
    Path        = "H:\$user"
    Destination = '~\Desktop\UserProfile\'
    Container   = $true
    Recurse     = $true
    Force       = $true
    Exclude     = '*$RECYCLE.BIN*'
}
Copy-Item @ciArgs

这将整个$user文件夹复制到您的UserProfile文件夹,而跳过回收站隐藏文件夹。

关于powershell - Powershell复制项目问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52105262/

相关文章:

powershell - 在 PowerShell 作业中使用本地函数

powershell - 为什么在 PowerShell 中的 for 循环中省略变量会导致无限循环?

Azure Powershell - 如何获取订阅所有者电子邮件?

powershell - 获取在 PowerShell 中复制的文件列表

powershell - 将选定项目从一个文件夹复制到新文件夹

Powershell Copy-Item 但仅复制更改的文件

sql - 将连接变量从xml传递到Powershell

c# - 如何使用 Web API(cs 或 asp.net)调用 Powershell 脚本

powershell - 如何将文件和文件夹树复制到远程计算机?

powershell - Powershell复制项目退出代码1