How do you take file ownership with PowerShell?

Question:

Issue: I wish to programmaticly (with PowerShell) take ownership of a file that I have absolutely no permissions on.

Update: I’ve thoroughly rewritten the question to give steps to reproduce the issue. Here’s what I’m doing:

This is throwing the error:

Additional notes:

  • $error[0].innerexception is null.
  • The steps taken as UserA ensured that userB has absolutely no permissions on C:\temp\file.txt.
  • running [System.IO.File]::GetAccessControl($path) throws a similar
    error (which is expected)
  • I am of course right-clicking PowerShell and choosing “Run as Administrator”.
  • I’ve tried disabling UAC, but that does not make a difference.
  • I can take ownership through the GUI so there should be a way to do this programmatically with PowerShell.

What am I doing wrong?

Update and answer:

The accepted answer I posted, works, but seems to be overkill. Simply referencing the file via a UNC path seems to do the trick:

Answer:

I’ve figured this out. I can only be done with SE_RESTORE_NAME permissions.

I learned how to do that from:
http://cosmoskey.blogspot.com/2010/07/setting-owner-on-acl-in-powershell.html

And with a comment from: http://fixingitpro.com/2011/07/08/set-owner-with-powershell-%E2%80%9Cthe-security-identifier-is-not-allowed-to-be-the-owner-of-this-object%E2%80%9D/

Here’s the full command that works for me:

Source:

How do you take file ownership with PowerShell? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply