Here are some different ways to solve this.
The powershell way:
$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.
The output-cmdscript way:
$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.
The output-cmdscript-variables way:
$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]
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.
Answer from pelin72 on Stack OverflowVideos
You can retrieve the root (parent) directory of all user profile directories from the registry as follows:
$profilesRootDir =
Get-ItemPropertyValue 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' ProfilesDirectory
To get a specific user's profile directory, say jdoe, you can then use:
# See more robust alternative below.
Join-Path $profilesRootDir jdoe
However, the ultimate source of truth is the ProfileImagePath value in the subkeys of the above registry key path, named for each user's SID (security identifier), which Get-LocalUser does provide (the output objects have a .SID property).
Thus, it is better to use:
Get-ItemPropertyValue "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$((Get-LocalUser jdoe).SID)" ProfileImagePath
To reliably get the profile directories of all enabled local users, use the following:
Get-LocalUser |
Where-Object Enabled |
ForEach-Object {
# Note the use of ...\ProfileList\
_.SID) and value name ProfileImagePath
Get-ItemPropertyValue "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\
_.SID)" ProfileImagePath
}
Perhaps something along this line:
$users = Get-LocalUser | Where-Object Enabled -eq true
$profilesRootDir = @(
Get-ItemPropertyValue 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' ProfilesDirectory)
ForEach ($User in $Users) {
$UserPath = Join-Path -Path "$profilesRootDir" -ChildPath "$User"
"User : $User`n" +
"User-Path: $UserPath"
}
Output:
User : Bruce
User-Path: C:\Users\Bruce
Get-ciminstance win32_computersystem | select username
will give you the currently logged in user, but what about all accounts, currently logged in or not.
Thanks!
Hello Powershellers
In my current environment, there are several users who’s profile folder name is different than their username due to AD changes. So I’m looking for a way to find the profile path of the logged in user and I can’t necessarily rely on the username. The other twist is that the power shell script is run by third party software and I’m not sure which service it runs as because $env:Userprofile variable doesn’t work when using the software. Any other ideas on how I can do this? My end goal is to copy a shortcut that is under a users profile to the startup folder.
In powershell, use the Win32_UserProfile WMI Object to find profiles remotely:
gwmi -ComputerName <computername> Win32_UserProfile
To find user profiles not on a server (or that are, either way), you could do something like:
gwmi -ComputerName <computername> Win32_UserProfile | ? {"C:\Users\<username>" -contains $_.LocalPath}
If the path exists, it will give results if not then it won't. You can do fancier stuff than this, but basically this should accomplish what you need without using regular expressions.
For current user run:
$env:USERPROFILE
To get list of all environmental variables, run:
Get-ChildItem Env: