Solved

Powershell Script to take ownership/copy/delete local folder

Posted on 2015-01-27
10
687 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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
 

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
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…
The viewer will learn how to count occurrences of each item in an array.

808 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