[Help] Powershell Script [Help]

Hi all,

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.

recursively copy everything under C:\Users\Test to C:\Script Testing\
get-childitem "C:\Users\Test" | % { 
    copy-item $_.FullName -destination "C:\Script Testing\$_" -recurse 
}

 recursively remove everything under C:\Users\Test
get-childitem "C:\Users\Test" -recurse | % { 
    remove-item $_.FullName -recurse 
}

Anyone got any ideas?

Thanks.

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.

There are PoSH commands to create/delete local user accounts. Once an account has been backed up these might help you achieve something akin to what you want to do: https://mcpmag.com/articles/2015/05/07/local-user-accounts-with-powershell.aspx

1 Like

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?

Hi,

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;

move-item "C:\Users\Test" -destination "C:\Script Testing\$_"

Hopefully that will get you a bit closer to what you need to do. Post back if it works, or if you solve it another way.

Cheers

1 Like

Hi,

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"

New-Item -ItemType directory -path $destination

robocopy C:\Users\$ProfileName\ $destination /MIR /xj

Recursively remove all user profile data

remove-item C:\Users\$ProfileName -forc

It's running perfectly except when it starts to delete the data I get:

remove-item : Access to the path 'C:\Users\Test\AppData\Local\Application Data' is denied.
At C:\Users\Desk\Desktop\Reprofile.ps1:11 char:1
+ remove-item C:\Users\$ProfileName -force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Users\Test:String) [Remove-Item], UnauthorizedAccessExcep
tion
+ FullyQualifiedErrorId : RemoveItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.RemoveItemComm
and

But the user is not logged in - Any ideas?

Thanks,

Hmmm... I'm not sure, I thought you might run into a problem like that.

I think you need to remove the local acount first? This link looks like it might have some stuff in it that might help;

1 Like

SUCCESS! Thanks for your help, I've managed to get it all working.

Really appreciate your time.

Can we see the finished script?

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.


http://pastebin.com/u3pz4HNS
2 Likes