Solved

Powershell Script to take ownership/copy/delete local folder

Posted on 2015-01-27
10
913 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 40

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 40

Expert Comment

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

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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 40

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 40

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 40

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
The viewer will learn how to dynamically set the form action using jQuery.

615 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