take ownership of files using powershell

I found the following script to take ownership of files. Could you please modify the script so that it can read csv input file with the PATH column and Users column which will assign the new owners for the shares specified in the excel. Thank you so much.

$FTPPath = 'c:\temp\test'
$acl = get-acl $FTPPath
$acl.SetOwner([System.Security.Principal.NTAccount] "domain\Caussyr")
set-acl $FTPPath $acl
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPRetiredCommented:
$ownerpath = import-csv -Path .\ownerpath.csv
<# Sample CSV
foreach ($opath in $ownerpath){
$acl = get-acl -Path $opath.path
$acl.SetOwner([System.Security.Principal.NTAccount] $opath.user)
set-acl $owner.path $acl 

Open in new window

creative555Author Commented:
Awesome!! Let me test out the script. I was reading somewhere that it was throwing some errors with powershell and that's why it was recommended to use other external utility such as takeown or subinacle. But I searched everywhere and didn't find any switches for those so that I can use excel file as import. It will take me forever to run line by line with takeownership or subinacle. So hopefully this will work.

Would import-csv work as well with takeown or subinacle util?
David Johnson, CD, MVPRetiredCommented:
if so then use powershell to parse the csv and pass parameters to the programs
start-process program.exe    $opath.path $opath.user
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

creative555Author Commented:
Here is the subniacle and takeown command.

subinacl /file test.txt /setowner=domain\foo


takeown /f \\Fileserver01\sales /r /A /d y

So I would do like this?
start-process subniacle   $opath.path $opath.user
creative555Author Commented:
Unfortunately, it didn't work: I got this error. Please help

ERROR: +     set-acl $owner.path $acl
ERROR: +             ~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidData: (:) [Set-Acl], ParameterBindingValidationException
ERROR:     + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetAclCommand
creative555Author Commented:
a friend sent me an example of vbscript below that is used to work with subinacl to do a loop. The vbscript does completely different thing but this is an example that subinacl could be used with vbscript or possibly powershell to loop. Since I am new to powershell and scripting, I would not be able to do this. Could you please help so that I can change owners for the files using csv import file that lists multiple shares and new owners.

subinacl /file c:\test\ntfs\ /setowner=Test

subinacl /file c:\test\ntfs\ /setowner=Test


strFilePath = "MSLogFile.log"
strOutFolderPath = "C:\BackupPermission"
strSubInAclPath = "C:\Program Files\Windows Resource Kits\Tools\SUBINACL.exe"

Set objFile = Createobject("Scripting.FileSystemObject")
set objTextFile = objFile.OpenTextFile(strFilePath,1)
Set WshShell = WScript.CreateObject("WScript.Shell")

If Not objFile.FolderExists(strOutFolderPath) Then
objFile.CreateFolder (strOutFolderPath)
End If

count = 0

Do Until objTextFile.AtEndOfStream
strLine = objTextFile.ReadLine
strLine = Split(strLine,",")(0)
If Trim(strLine) <> "" Then
  strOutLog = strOutFolderPath & "\" & Mid(strLine,InStrRev(strLine,"\") + 1,Len(strLine)) & ".txt"
  strCommand = Chr(34) & strSubInAclPath & Chr(34) & " /noverbose /outputlog=" & strOutLog & " /subdirectories " & strLine & " /display"
  wscript.echo strCommand
  Return = WshShell.Run(strCommand, 1, true)
End If

creative555Author Commented:
I found such a simple solution for takeown command.....I was overthinking...loops, vbscripts, powershell that didn't work.....I can just run line by line and paste it from excel file all the shares. I can use excel file and then concatenate all the rows with the command and paste it. This scripts works as a charm and re-assigns owners to Builtin\Administrators to all the folders that even got access denied.

takeown /f C:\Installs\1 /a /r
takeown /f C:\Installs\test /a /r

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
creative555Author Commented:
THis was a solution
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.