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 Overflow
🌐
Microsoft
devblogs.microsoft.com › dev blogs › scripting blog [archived] › use powershell to find user profiles on a computer
Use PowerShell to Find User Profiles on a Computer - Scripting Blog [archived]
November 10, 2011 - How Can I List All the User Profiles on a Computer? That post talks about enumerating a registry key to find the profile information. The registry location did not change in Windows 7, so the VBScript would still work. Here is the registry location: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList · The registry location viewed in the Registry Editor appears in the following figure. Using Windows PowerShell, it is really easy to get and to display registry keys.
🌐
Business.com
business.com › home › technology › it management
A Guide to Managing User Profiles With PowerShell
February 12, 2026 - Using PowerShell, administrators can retrieve detailed profile data with Get-CimInstance (recommended for modern systems) or Get-WmiObject (legacy). Figure 1 illustrates retrieving the first user profile on a local computer, where properties ...
Top answer
1 of 1
2

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.

Top answer
1 of 3
2

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
  }
2 of 3
1

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
🌐
Reddit
reddit.com › r/powershell › get userprofile path for current logged in user.
r/PowerShell on Reddit: Get userprofile path for current logged in user.
May 28, 2021 -

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.

🌐
Progress
progress.com › blogs › how-to-accurately-enumerate-windows-user-profiles-with-powershell
How To Enumerate Windows User Profiles With PowerShell
November 13, 2024 - By looping over each registry key with ForEach-Object and then calling the GetValue() method on each ProfileImagePath value, it will now return only the paths we're after. Once you've got the paths to each user profile, additional checks or codes can be performed on each folder. For example, each user's temp folder path is located in the AppData\Local\Temp folder. To enumerate every user's temp folder, you could add a command inside of your loop to list those files. PowerShell PS> Get-ChildItem 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList' | ForEach-Object { $profilePath = $_.GetValue('ProfileImagePath') Get-ChildItem -Path "$profilePath\AppData\Local\Temp" }
🌐
Super User
superuser.com › questions › 1478029 › powershell-get-user-profiles-name-from-windows-7-10
Powershell: get user profiles name from windows 7/10 - Super User
September 2, 2019 - Below is the command which I am using at the moment but it contains the system generated profiles too. Get-CimInstance -ClassName Win32_UserProfile | Add-Member -MemberType ScriptProperty -Name UserName -Value { (New-Object System.Security.Principal.SecurityIdentifier($this.Sid)).Translate([System.Security.Principal.NTAccount]).Value } -PassThru | Out-GridView · Any advise on this? windows-7 · windows · powershell ·
Find elsewhere
🌐
PowerShell Gallery
powershellgallery.com › packages › Get-UserProfile › 1.0.7 › Content › Get-UserProfile.psm1
PowerShell Gallery | Get-UserProfile.psm1 1.0.7
This site uses cookies for analytics, personalized content and ads. By continuing to browse this site, you agree to this use. Learn more · Get-UserProfile · 1.0.7 · Get-UserProfile.psm1 · Contact Us · Terms of Use · Gallery Status · Feedback · © 2026 Microsoft Corporation
🌐
Microsoft
devblogs.microsoft.com › dev blogs › scripting blog [archived] › powertip: use powershell to find user profile path
PowerTip: Use PowerShell to Find User Profile Path - Scripting Blog [archived]
February 19, 2014 - Summary: Use Windows PowerShell to find the user profile path. How can I easily get information about the folder and path to the profile for a currently signed-in user? Use the Env: PowerShell drive, and select the UserProfile environmental variable: $env:USERPROFILE
🌐
SharePoint Diary
sharepointdiary.com › sharepoint diary › user profile › sharepoint online: get user profile properties using powershell
SharePoint Online: Get User Profile Properties using PowerShell - SharePoint Diary
October 16, 2025 - To get all available properties from user profile service, use: $UserProfile.UserProfileProperties.Keys · Here is the PowerShell to get all user profiles in SharePoint Online.
🌐
Next of Windows
nextofwindows.com › home › how to get the list of user profiles on local and remote computer
How To Get the List of User Profiles on Local and Remote Computer - NEXTOFWINDOWS.COM
April 2, 2020 - Running the following snippet in your PowerShell window and see what you get. $path = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\*' Get-ItemProperty -Path $path | Select-Object -Property PSChildName, ProfileImagePath · What it does is to retrieve the ProfileList info from Windows Registry and return each user’s SID and its profile location.
🌐
4sysops
4sysops.com › home › blog › tips › remotely query user profile information with powershell
Remotely query user profile information with PowerShell – 4sysops
July 28, 2023 - The PowerShell script discussed in this article will help you in querying for information about Windows user profiles on remote computers. The script returns user name, profile type, and in-use status of Windows profiles.
Top answer
1 of 4
1
$ErrorActionPreference = "SilentlyContinue"$Path = "C:\Users"$UserFolders = Get-ChildItem -Path $Path -Directory$currentDate = Get-Date$ageLimit = 60# Set the execution policy to bypass (use with caution in production)Set-ExecutionPolicy Bypass -Force# Loop through each user profile folderForEach ($UserFolder in $UserFolders) {    $UserName = $UserFolder.Name    # Only process profiles that have an NTUser.dat file    $NTUserPath = "$Path\$UserName\NTUSER.DAT"    If (Test-Path $NTUserPath) {        $Dat = Get-Item $NTUserPath -Force        $DatTime = $Dat.LastWriteTime        # Reset NTUSER.DAT's LastWriteTime to match the folder's LastWriteTime        If ($UserFolder.Name -ne "default") {            $Dat.LastWriteTime = $UserFolder.LastWriteTime        }    }    # Calculate profile age and delete if older than $ageLimit    $profileAge = ($currentDate - $UserFolder.LastWriteTime).Days    If ($profileAge -ge $ageLimit) {        # Remove user profile and any content inside it (use with caution)        Try {            Remove-Item -Path $UserFolder.FullName -Recurse -Force            Write-Host "Deleted profile: $UserName"        } Catch {            Write-Host "Error deleting profile: $UserName - $_"        }    }}# Optional: Clean up empty folders (if necessary)$EmptyFolders = Get-ChildItem -Path $Path -Directory | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }ForEach ($EmptyFolder in $EmptyFolders) {    Try {        Remove-Item -Path $EmptyFolder.FullName -Force        Write-Host "Deleted empty folder: $($EmptyFolder.Name)"    } Catch {        Write-Host "Error deleting empty folder: $($EmptyFolder.Name) - $_"    }}Write-Host "Profile cleanup complete."
2 of 4
0
Your script is on the right track for deleting old user profiles, but there are a few improvements that can make it more efficient, avoid some pitfalls (such as leaving empty folders), and potentially clean up obsolete or unnecessary parts of the code. Here's an enhanced version of the script, with some optimizations and added checks:Suggestions & Improvements:Use -Directory when getting user profiles: You can directly filter for directories with -Directory in the Get-ChildItem cmdlet, reducing the need for additional checks.Ensure NTUser.dat exists: You don't need to call Get-Item multiple times in a loop if you can check for existence first. I’ve added that check with Test-Path.Handle empty profile folders: When a profile folder is removed, we should check if it’s empty before attempting to remove it. If it's empty, you can remove it with Remove-Item as well.Use -Force for visibility of hidden files: This ensures hidden files like NTUser.dat are also handled properly.Remove the report object: If you're not using $Report, it can be omitted.Ensure execution policy is set before script runs: It’s more logical to set the execution policy at the start, so it’s enforced before running any other script actions.
🌐
SharePoint Diary
sharepointdiary.com › sharepoint diary › user profile › get and export user profile properties using powershell in sharepoint server
Get and Export User Profile Properties using PowerShell in SharePoint Server - SharePoint Diary
September 30, 2025 - You are not limited to these fields, but there are plenty more fields in User Profiles. To get all available user profile properties, use: ... Let’s use PowerShell to get all user profile properties and export them to CSV.
🌐
TechTarget
techtarget.com › searchwindowsserver › tutorial › How-to-find-and-customize-your-PowerShell-profile
How to find and customize your PowerShell profile | TechTarget
To locate the PowerShell profile, enter the $Profile variable to show the path. In this case, the username is hidden.
Published   September 16, 2025
🌐
Contoso Corporation
pnp.github.io › powershell › cmdlets › Get-PnPUserProfileProperty.html
Get-PnPUserProfileProperty | PnP PowerShell
Get-PnPUserProfileProperty -Account 'user@domain.com' -Properties 'FirstName','LastName' Returns the FirstName and LastName profile properties for the specified user
🌐
Microsoft
devblogs.microsoft.com › dev blogs › scripting blog [archived] › use powershell to find detailed windows profile information
Use PowerShell to Find Detailed Windows Profile Information - Scripting Blog [archived]
March 4, 2013 - To find user profile information, I use the Win32_UserProfile WMI class. To query the Win32_UserProfile WMI class, I can use the Get-WmiObject cmdlet.