Folder Ownership

I am trying to match the SAM account name with the users home folder. Some home folders are not owned by the user.  Therefore I am extracting the SAM ID and trying to match the SAM ID to the folder name and give the user Ownership permissions .

At the moment I have the following :

# Use SAM account and take ownership of user folder

$ADUser = Get-Content 'C:\Temp\SecTest\AdUsers.txt'

$HomeFolders = Get-ChildItem 'C:\temp\SecTest' | Where-Object { $_.PSIsContainer -eq $true }

foreach ($HomeFolder in $Homefolders)

{

    $User = Get-ADUser $ADUser | Select SID
    get-item $ADUser | set-owner -account $User.SID

}

When I run the above I get the following error :

 Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADUser' required by parameter 'Identity'. Specified
method is not supported.

I am new to Powershell, but I think I may need to convert this to a string ??
CaussyRAsked:
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.

prashanthdCommented:
Try the following

# Use SAM account and take ownership of user folder

$ADUsers = Get-Content 'C:\Temp\SecTest\AdUsers.txt'

#$HomeFolders = Get-ChildItem 'C:\temp\SecTest' | Where-Object { $_.PSIsContainer -eq $true }
foreach ($aduser in $adusers)
{
    $User = Get-ADUser $ADUser | Select SID
    get-item $ADUser | set-owner -account $User.SID
}
0
CaussyRAuthor Commented:
I didn't get an error because the script didn't do anything. :o(

All I need to do is match the SAM ID with the home folder and change the ownership to the same as the SAM ID.  For example a user named test1 has a home folder 'test1'. But the owner is Administrator of the home folder. I wasn't to change the home folder owner to the user account test1.

I know the set-owner command works as I have used this in a previous script, which worked for single users as I created them but now I'm looking at 100+ users.
0

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
CaussyRAuthor Commented:
This is working solution
0
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
Powershell

From novice to tech pro — start learning today.