Solved

powershell for home folder owner change

Posted on 2016-07-21
4
38 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 12

Assisted Solution

by:Dustin Saunders
Dustin Saunders earned 500 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 12

Accepted Solution

by:
Dustin Saunders earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article explains how to install and use the NTBackup utility that comes with Windows Server.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
This tutorial will walk an individual through the process of installing of Data Protection Manager on a server running Windows Server 2012 R2, including the prerequisites. Microsoft .Net 3.5 is required. To install this feature, go to Server Manager…

789 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