Solved

Powershell Script to take ownership/copy/delete local folder

Posted on 2015-01-27
10
639 Views
Last Modified: 2015-02-02
I have the script below which I need to take ownership of the folder, copy it to a NAS and then delete it. I can't seem to get the take ownership function to work. Please help! Thanks!

$user = Read-Host 'Enter Username of Profile to Delete'
If ($user -ne $null){
$Folder = "D:\RDSProfiles\$user.PHI.V2"
    If (Test-path $Folder){
    takeown /f $Folder /R /D Y
    Copy-Item -Path "$Folder" -Force -Recurse -Destination "\\PISNAS1\User Archive\\"
    Remove-Item $Folder -Recurse -Force
      }
}
0
Comment
Question by:TheNetworkPro15
  • 5
  • 5
10 Comments
 
LVL 39

Expert Comment

by:footech
ID: 40574070
Try using the /A switch to give ownership to the Administrators group.
takeown /f $Folder /R /A /D Y

Open in new window

0
 

Author Comment

by:TheNetworkPro15
ID: 40576278
It seems like the recursion is not working. It will copy the root level folders but errors out on any contents below that. I'll try to run it again and see if I can give specific errors.
0
 
LVL 39

Expert Comment

by:footech
ID: 40576516
Taking ownership may not be enough.  You may have to adjust permissions.
0
 

Author Comment

by:TheNetworkPro15
ID: 40577744
Would you be able to add the necessary lines in there to give "PHI\gocadmin" full permissions to all files and folders?
0
 
LVL 39

Expert Comment

by:footech
ID: 40577976
You could try adding the following after the takeown command
icacls $folder /grant "PHI\gocadmin:(OI)(CI)(F)" /T
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:TheNetworkPro15
ID: 40578725
I added the line and it seemed to work better but it's still giving errors when it gets down to the file level. I look at the NTFS permissions on the files and the new ownership and gocadmin permissions did not apply. I attached the log so maybe that will be helpful.

The script looks like this now.

$user = Read-Host 'Enter Username of Profile to Delete'
If ($user -ne $null){
$Folder = "D:\RDSProfiles\$user.PHI.V2"
    If (Test-path $Folder){
    takeown /f $Folder /R /A /D Y
    icacls $folder /grant "PHI\gocadmin:(OI)(CI)(F)" /T
    Copy-Item -Path "$Folder" -Force -Recurse -Destination "\\PISNAS1\User Archive\\"
    Remove-Item $Folder -Recurse -Force
      }
}
Errors.docx
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 40578786
If ownership and permissions didn't apply, then it's likely an inheritance issue.  The only way I know to deal with that is to process each file individually.  You might try adding the following after the icacls line.
Get-ChildItem $folder -force -recurse | Select -exand Fullname | % `
{
    takeown /f $_ /A /D Y
    icacls $_ /grant "PHI\gocadmin:(F)"
}

Open in new window


If you want to cut down on some of the output, you can add the /Q switch to icacls.
Permissions can be a pain.  It seems like every time it's a different scenario when dealing with them in batch, depending on what existed before copying/deleting.
0
 

Author Comment

by:TheNetworkPro15
ID: 40580336
So to clarify, my new script should look like this?

$user = Read-Host 'Enter Username of Profile to Delete'
If ($user -ne $null){
$Folder = "D:\RDSProfiles\$user.PHI.V2"
    If (Test-path $Folder){
    takeown /f $Folder /R /A /D Y
    icacls $folder /grant "PHI\gocadmin:(OI)(CI)(F)" /T
Get-ChildItem $folder -force -recurse | Select -exand Fullname | % `
{
    takeown /f $_ /A /D Y
    icacls $_ /grant "PHI\gocadmin:(F)"
}
    Copy-Item -Path "$Folder" -Force -Recurse -Destination "\\PISNAS1\User Archive\\"
    Remove-Item $Folder -Recurse -Force
      }
}
0
 
LVL 39

Expert Comment

by:footech
ID: 40580397
Yes, that's right.
Quick suggestion - in general when posting any code here longer than a line or two, try to use the code block feature to format your text (just select the pertinent text, then click the "Code" formatting item).
0
 

Author Comment

by:TheNetworkPro15
ID: 40584691
That last bit of code seems to have done the trick. Thanks for the help!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
This article will help you understand what HashTables are and how to use them in PowerShell.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now