Powershell ROBOCOPY 用户数据到映射的主驱动器

Powershell ROBOCOPY Users Data to Mapped Home Drive

%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

你能检查一下 UserProfile 环境变量是否有效吗?

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

%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