This function allows you to change the owner of a file(s) or folder(s) to another user or group. This is similiar to the takeown.exe command that is available from the command prompt, but in PowerShell. The function will elevate the token of the account (only in console session) running this command to allow access to change the ownership of the file or folder. The default value of -Account is 'Builtin\Administrators' but can be changed to any allowed user or group. You can also recursively go through files and folders and apply the new ownership as well.

Related blog post: http://learn-powershell.net/2014/06/24/changing-ownership-of-file-or-folder-using-powershell/

Remember to dot source the script to load the function into the console session.

 

 

PowerShell
Edit|Remove
. .\Set-Owner.ps1

 

 Change ownership of folder and subfolders/files.

 

PowerShell
Edit|Remove
Set-Owner -Path C:\temp -Recurse 
 Allow for pipeline input and set owner to another account.
PowerShell
Edit|Remove
Get-ChildItem C:\Temp |  
Set-Owner -Recurse -Account 'Domain\bprox'