Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell Script to take ownership/copy/delete local folder

Posted on 2015-01-27
10
Medium Priority
?
1,130 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 41

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 41

Expert Comment

by:footech
ID: 40576516
Taking ownership may not be enough.  You may have to adjust permissions.
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 

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 41

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
 

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 41

Accepted Solution

by:
footech earned 2000 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 41

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The viewer will learn how to count occurrences of each item in an array.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

916 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