Powershell ROBOCOPY用户数据映射到家庭驱动器

时间:2016-06-01 03:12:30

标签: powershell robocopy user-profile

当我想将此部署到多个用户时,

%Username%似乎失败了。有一个更好的方法吗?获取指定用户配置文件的方法是?:

New-Item -ItemType directory -Path O:\_Backups\Contacts
Robocopy C:\Users\%Username%\Contacts O:\_Backups\Contacts /xo

New-Item -ItemType directory -Path O:\_Backups\Desktop
Robocopy C:\Users\%Username%\Dekstop O:\_Backups\Desktop /xo

New-Item -ItemType directory -Path O:\_Backups\Documents
Robocopy C:\Users\%Username%\Documents O:\_Backups\Documents /xo

New-Item -ItemType directory -Path O:\_Backups\Favorites
Robocopy C:\Users\%Username%\Favorites O:\_Backups\Favorites /xo

New-Item -ItemType directory -Path O:\_Backups\Pictures
Robocopy C:\Users\%Username%\Pictures O:\_Backups\Pictures /xo

2 个答案:

答案 0 :(得分:1)

您可以检查UserProfile环境变量是否有效吗?

Robocopy $env:USERPROFILE\Contacts D:\_Backups\Contacts /xo

答案 1 :(得分:0)

%envVariableName%不是使用环境变量的格式。 $env:variableName是PowerShell格式。

New-Item -ItemType directory -Path O:\_Backups\Contacts
Robocopy "C:\Users\$env:username\Contacts" O:\_Backups\Contacts /xo

New-Item -ItemType directory -Path O:\_Backups\Desktop
Robocopy "C:\Users\$env:username\Dekstop" O:\_Backups\Desktop /xo

New-Item -ItemType directory -Path O:\_Backups\Documents
Robocopy "C:\Users\$env:username\Documents" O:\_Backups\Documents /xo

New-Item -ItemType directory -Path O:\_Backups\Favorites
Robocopy "C:\Users\$env:username\Favorites" O:\_Backups\Favorites /xo

New-Item -ItemType directory -Path O:\_Backups\Pictures
Robocopy "C:\Users\$env:username\Pictures" O:\_Backups\Pictures /xo