Reputation: 65
I have the following Command and it gets the username:
((Get-WMIObject -ClassName Win32_ComputerSystem).Username).Split('\')[1]
The trouble I am having is figuring out how to have a letter (Value) assigned to each profile
Example:
User1
User2
User3
User1=%A%
User2=%B%
User3=%C%
so I can use the variables in a later part of the script.
I have tried adding code, looking up similar commands and scripts with no luck.
Can someone please tell me where I went wrong?
Upvotes: 0
Views: 2759
Reputation: 93
Here are some different ways to solve this.
$admin=get-credential -Message "Admin credentials"
$paths=Get-WMIObject -ClassName Win32_UserProfile -Filter "special=false and localpath like 'C:\\users\\%'" -Property localpath |select -ExpandProperty localpath
$destination="\\server\d$\desktopbackups"
foreach ($source in $paths) {
$user = Split-Path -Path $source -leaf
$sourceDesktop = join-path $source "Desktop"
$DestFolder = Join-Path $destination $user
write-host -ForegroundColor Yellow "Backing up $sourceDesktop to folder $destfolder"
# First delete the destination
remove-item $destfolder -Filter *.* -Recurse -Credential $admin -WhatIf
# Copy the files
Copy-Item $sourceDesktop $DestFolder -Recurse -Credential $admin -WhatIf
}
The above solution allows you to enter the admin credentials and the file operations are done as your admin user. You don't have to log in as a different user to use this.
$paths=Get-WMIObject -ClassName Win32_UserProfile -Filter "special=false and localpath like 'C:\\users\\%'" -Property localpath |select -ExpandProperty localpath
$destination="\\server\d$\desktopbackups"
$cmdscriptfile="c:\temp\backupDesktops.cmd"
foreach ($source in $paths) {
$user = Split-Path -Path $source -leaf
$srcfolder = join-path $source "Desktop"
$DestFolder = Join-Path $destination $user
"xcopy $srcfolder $Destfolder" | out-file -Append $cmdscriptfile
}
The above script creates a cmd-script which you can log in as admin and run.
$paths=Get-WMIObject -ClassName Win32_UserProfile -Filter "special=false and localpath like 'C:\\users\\%'" -Property localpath |select -ExpandProperty localpath
$cmdscriptfile="c:\temp\backupDesktops.cmd"
Clear-Content $cmdscriptfile
$i=65
foreach ($source in $paths) {
$user = Split-Path -Path $source -leaf
"set {0}={1}" -f [char]$i,$user | out-file -Append $cmdscriptfile
$i++
}
"xcopy c:\users\%A%\Desktops ..."|Out-File -Append $cmdscriptfile
The above lets you edit the cmd file and use the different variables for the paths. This is what you asked for (if I understood correctly).
Personally I would use Powershell.
Upvotes: 2