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

x
?
Solved

powershell for home folder owner change

Posted on 2016-07-21
4
Medium Priority
?
131 Views
Last Modified: 2016-07-21
Dear Experts,

Could you please help me with this.

I have over 500  user  home folders on a file share

Current owner of some of those home folders are  not the  actual user owners.  I can see that administrator as current owner on some and some other names as owner of some others home folders.  So it is a bit mess to me.

during the account creation, home folder names gets added as %username% in the home folder section in AD.  

For reporting purposes ( to identify who is owner of another home folder)  and to assign ownership of home folders to actual users, Could you please provide me with a PowerShell to run against   \\server\share\users\home ?

for an example;

Mike has a home folder called Mike.Buck   if his home folder's owner is administrator or another user, I would like to be informed about it on a excel and meanwhile, current owner must be changed to Mike.Buck.  I believe Mike must also have full ntfs permissions to his home folder.

thanks
0
Comment
Question by:kuzum
  • 2
  • 2
4 Comments
 
LVL 14

Assisted Solution

by:Dustin Saunders
Dustin Saunders earned 2000 total points
ID: 41723097
Something like this should work:

$outFile = "C:\Users\Administrator\Desktop\owner.csv"

Add-Content -Path $outFile -Value "Folder,Owner"

Import-Module ActiveDirectory

$users = Get-ADUser -Filter * -Properties homeDirectory | select samaccountname,homeDirectory

foreach ($user in $users)
{
    if ($user.homeDirectory)
    {
    Write-Host $user.homeDirectory
    $ACL = Get-Acl $user.homeDirectory
    $owner = $ACL.GetOwner([System.Security.Principal.NTAccount]).ToString()
    $owner2 = $owner.Split('\')
    if ($owner2[1] -ne $user.samaccountname.ToString())
    {
        
        
        try
        {
            $ar = New-Object System.Security.AccessControl.FileSystemAccessRule($user.samaccountname, 'FullControl', 'ContainerInherit,ObjectInherit','None','Allow')
            $acl.SetAccessRule($ar)
            $ACL.SetOwner([System.Security.Principal.NTAccount]$user.samaccountname)
            Set-Acl -Path $user.homeDirectory -AclObject $ACL -whatif
            $outContent = $user.homeDirectory + ",Owner was " + $owner2[1] + ".  Set to " + $user.samaccountname + "."
            Add-Content -Path $outFile -Value $outContent
            
        }
        catch
        {
           $outContent = $user.homeDirectory + ",Owner was " + $owner2[1] + ", but I couldn't set it to " + $user.samaccountname + "."
            Add-Content -Path $outFile -Value $outContent
        }
    }

    }
}

Open in new window


It's in whatif mode, so you can remove "-whatif" from this time if the results look correct.
Set-Acl -Path $user.homeDirectory -AclObject $ACL -whatif

Open in new window

0
 

Author Comment

by:kuzum
ID: 41723350
Dustin,

this is just excellent!  output was as I expected.

I'm assuming it is only exporting home folders that currently owned by others and not exporting Mike.Buck as Mike already owner of his home folder?

One small favour please, if I wanted to run this against a specific OU in AD ( I want to do 100 users a day),  could you modify it for me?
0
 
LVL 14

Accepted Solution

by:
Dustin Saunders earned 2000 total points
ID: 41723454
Yeah, you can do that with a SearchBase (just change the one in the sample here to the OU you want to do).

$outFile = "C:\Users\Administrator\Desktop\owner.csv"
$searchBase = "OU=CompanyA,OU=Clients,OU=zUsers,DC=WizDev,DC=Local"

Add-Content -Path $outFile -Value "Folder,Owner"

Import-Module ActiveDirectory

$users = Get-ADUser -Filter * -SearchBase $searchBase -Properties homeDirectory | select samaccountname,homeDirectory

foreach ($user in $users)
{
    if ($user.homeDirectory)
    {
    Write-Host $user.homeDirectory
    $ACL = Get-Acl $user.homeDirectory
    $owner = $ACL.GetOwner([System.Security.Principal.NTAccount]).ToString()
    $owner2 = $owner.Split('\')
    if ($owner2[1] -ne $user.samaccountname.ToString())
    {
        
        
        try
        {
            $ar = New-Object System.Security.AccessControl.FileSystemAccessRule($user.samaccountname, 'FullControl', 'ContainerInherit,ObjectInherit','None','Allow')
            $acl.SetAccessRule($ar)
            $ACL.SetOwner([System.Security.Principal.NTAccount]$user.samaccountname)
            Set-Acl -Path $user.homeDirectory -AclObject $ACL -whatif
            $outContent = $user.homeDirectory + ",Owner was " + $owner2[1] + ".  Set to " + $user.samaccountname + "."
            Add-Content -Path $outFile -Value $outContent
            
        }
        catch
        {
           $outContent = $user.homeDirectory + ",Owner was " + $owner2[1] + ", but I couldn't set it to " + $user.samaccountname + "."
            Add-Content -Path $outFile -Value $outContent
        }
    }

    }
}

Open in new window

0
 

Author Closing Comment

by:kuzum
ID: 41723567
excellent result.
0

Featured Post

Technology Partners: 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

Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

773 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