How do you take file ownership with PowerShell?


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:


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

I learned how to do that from:

And with a comment from:

Here’s the full command that works for me:


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

Leave a Reply