title: PowerShell Change owner of files and folders
body: <p>Searching the web, I found 2 scripts that are able to change the owner of files and folders. When testing this, it functions perfectly in PowerShell 1.0. Now I'm trying to combine both so they work recursively, because we have folders with over 500 sub directories and files in them. And it's a tremendous job to do..</p>
  
  <p><strong>We want to:</strong></p>
  
  <ul>
  <li>Run one script on <code>\\server\C$\Folder</code> (without using external tools)</li>
  <li>to change the owner of all files and subfolders to <code>BUILTIN\Administrators</code></li>
  </ul>
  
  <p><strong>The problem:</strong></p>
  
  <ul>
  <li>Each script only works for 1 file or 1 folder. How can this be combined in one script so it does all the subfoldes and files all together? Putting it in 2 different functions maybe and loop through it or..</li>
  </ul>
  
  <p><strong>Script1 : Change FILE owner to Admin</strong></p>
  
  <pre><code>$File = "\\server\c$\Users\dir\Downloads\Target\TargetFile.txt"
  $Account = New-Object System.Security.Principal.NTAccount("BUILTIN\Administrators")
  $FileSecurity = new-object System.Security.AccessControl.FileSecurity
  $FileSecurity.SetOwner($Account)
  [System.IO.File]::SetAccessControl($File, $FileSecurity)
  </code></pre>
  
  <p><strong>Script2 : Change FOLDER owner to Admin</strong></p>
  
  <pre><code>$AdjustTokenPrivileges = @"
  using System;
  using System.Runtime.InteropServices;
  
   public class TokenManipulator
   {
    [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
    internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
    ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
    [DllImport("kernel32.dll", ExactSpelling = true)]
    internal static extern IntPtr GetCurrentProcess();
    [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
    internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr
    phtok);
    [DllImport("advapi32.dll", SetLastError = true)]
    internal static extern bool LookupPrivilegeValue(string host, string name,
    ref long pluid);
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    internal struct TokPriv1Luid
    {
     public int Count;
     public long Luid;
     public int Attr;
    }
    internal const int SE_PRIVILEGE_DISABLED = 0x00000000;
    internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
    internal const int TOKEN_QUERY = 0x00000008;
    internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
    public static bool AddPrivilege(string privilege)
    {
     try
     {
      bool retVal;
      TokPriv1Luid tp;
      IntPtr hproc = GetCurrentProcess();
      IntPtr htok = IntPtr.Zero;
      retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
      tp.Count = 1;
      tp.Luid = 0;
      tp.Attr = SE_PRIVILEGE_ENABLED;
      retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
      retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
      return retVal;
     }
     catch (Exception ex)
     {
      throw ex;
     }
    }
    public static bool RemovePrivilege(string privilege)
    {
     try
     {
      bool retVal;
      TokPriv1Luid tp;
      IntPtr hproc = GetCurrentProcess();
      IntPtr htok = IntPtr.Zero;
      retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
      tp.Count = 1;
      tp.Luid = 0;
      tp.Attr = SE_PRIVILEGE_DISABLED;
      retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
      retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
      return retVal;
     }
     catch (Exception ex)
     {
      throw ex;
     }
    }
   }
  "@
  add-type $AdjustTokenPrivileges
  $Folder = Get-Item "C:\Users\dir\Downloads\Target"
  [void][TokenManipulator]::AddPrivilege("SeRestorePrivilege") 
  [void][TokenManipulator]::AddPrivilege("SeBackupPrivilege") 
  [void][TokenManipulator]::AddPrivilege("SeTakeOwnershipPrivilege") 
  $NewOwnerACL = New-Object System.Security.AccessControl.DirectorySecurity
  $Admin = New-Object System.Security.Principal.NTAccount("BUILTIN\Administrators")
  $NewOwnerACL.SetOwner($Admin)
  $Folder.SetAccessControl($NewOwnerACL)
  </code></pre>