PowerShell script to create a new user profile and copy all data from the current profile to the new profile
Here’s a PowerShell script to create a new user profile and copy all data from the current profile to the new profile. This process involves the creation of the new user account, setting permissions, and copying files from the old profile folder to the new one.
# Get new username and password
$NewUsername = Read-Host "Enter the new username"
$NewPassword = Read-Host "Enter the new password" -AsSecureString
# Create a new local user account
Write-Host "Creating new user account '$NewUsername'..."
try {
New-LocalUser -Name $NewUsername -Password $NewPassword -PasswordNeverExpires -AccountNeverExpires -UserMayNotChangePassword -FullName "New User" -Description "Newly created profile"
Add-LocalGroupMember -Group "Administrators" -Member $NewUsername
Write-Host "New user account '$NewUsername' created successfully and added to Administrators group."
} catch {
Write-Host "Failed to create the new user account: $_"
exit
}
# Get current user profile path
$CurrentUsername = $env:USERNAME
$CurrentProfilePath = [System.IO.Path]::Combine($env:SystemDrive, "Users", $CurrentUsername)
$NewProfilePath = [System.IO.Path]::Combine($env:SystemDrive, "Users", $NewUsername)
# Check if current profile exists
if (-Not (Test-Path $CurrentProfilePath)) {
Write-Host "The current user profile path '$CurrentProfilePath' does not exist. Exiting."
exit
}
# Copy data from the current profile to the new profile
Write-Host "Copying data from '$CurrentProfilePath' to '$NewProfilePath'..."
# Create the new user's profile folder by logging in as the new user
try {
Write-Host "Creating profile folder for '$NewUsername'..."
Start-Process "C:\Windows\System32\runas.exe" -ArgumentList "/user:$NewUsername explorer.exe"
Write-Host "Profile folder created for '$NewUsername'. Please log in once as the new user and then re-run this script to continue copying files."
exit
} catch {
Write-Host "Failed to create profile folder for the new user. Exiting."
exit
}
# After the new user has logged in at least once, proceed with copying files
# Copy files from the current profile to the new profile, excluding system files
Write-Host "Copying files to the new profile..."
try {
Copy-Item -Path "$CurrentProfilePath\*" -Destination $NewProfilePath -Recurse -Force -Exclude "ntuser.dat*", "AppData"
Write-Host "Data copied successfully from the current profile to the new profile."
} catch {
Write-Host "Failed to copy the files: $_"
}
# Set permissions on the new profile folder for the new user
Write-Host "Setting permissions for '$NewUsername' on the new profile folder..."
try {
$acl = Get-Acl $NewProfilePath
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$NewUsername", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)
Set-Acl -Path $NewProfilePath -AclObject $acl
Write-Host "Permissions set successfully for '$NewUsername'."
} catch {
Write-Host "Failed to set permissions: $_"
}
Write-Host "New user profile setup complete."
How the script works:
- Create a new user account: The script prompts for the new username and password, then creates the new user and adds it to the Administrators group.
- Create profile folder: The script initiates a profile folder for the new user by running
explorer.exe
under the new user’s credentials. This step requires the user to log in once as the new user to create the profile. - Copy user data: After the new profile folder is created, it copies all data from the current profile to the new profile, excluding system files such as
ntuser.dat
and theAppData
folder to avoid potential issues. - Set permissions: The script adjusts file permissions on the new profile folder so that the new user has full control over their profile files.
Steps to run:
- Open PowerShell as Administrator.
- Run the script or save it as a
.ps1
file and execute it. - Log in once as the new user (this is necessary to create the profile folder).
- Re-run the script to complete the data copying.
Let me know if you need any further adjustments!