I'm attempting to create a PS script that will copy a user profile and all it's contents to say C:\ProfileBAK and then delete the original in C:\Users so when the user logs back in all previous data/settings etc. has been removed but can be restored if required.
I've trailed the web haven't managed to get very far because... 1) I suck at scripting but I'm learning and 2) I can't find something that really matches what I'm after.
EDIT: I've managed to find this however, it only copies the contents rather than the full folder.
I think you will struggle to delete the entire contents of a users folder. There are hidden directories like AppData that stores information you won't be able to delete - because it is in use. To do what you want the script would need to be executed from a different account with the target account logged out.
But you could kind of do what you want if you are just dealing with the contents of folders like Documents and Music (provided no files are in use).
Personally I would probably use robocopy to backup the folder though, but you could still wrap it in PoSH if you needed to do something useful like datestamp the backup folders.
Thanks for the response, the script would be ran when the [to be deleted user] was logged off so from what I can see based on my tests with the script I've got so far (in the edit) it works but only copies the contents of the folder, do you know how I could make the change(s) so it copies the full folder + said contents?
Like I said, generally if I am backing up a users directory I nearly always use the Robocopy command. That way I can have it retry if a file is locked and get all permissions/properties mirrored. There shouldn't be any problem calling robocopy from powershell if you need to.
However, whilst your copy command is fine for copying the child-items of a directory the easiest way to move an entire directory including its contents (which is what you are trying to do I think) is;
I took your advice and converted xcopy to robocopy and also changed a few other sections. I've created the variables and the script now also prompts the user to select the Profile Name to backup/delete.
Copy specified user profile data $computername = $env:COMPUTERNAME $ProfileName = Read-Host "Profile Name?" $destination = "C:\Test\$ProfileName"
Of course. It will basically prompt for the User Profile name, backup to a location of choosing (I've tested locally and to a network location) and then delete the profile including all registry entries.