Discussion Locked

Automate the changing of ownership of files and folders in Windows 10

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I know how to manually change file/folder ownership within Windows, what I want to find out is if there is a script or other means to which when a file/folder is copied from one user to another, the ownership of the said data changes to that of the new location. Thus, manual changing is not required.

3 replies

by kees_b edited

I doubt if you can download such a script. But, since it's can be done in PowerShell, you can write your own.

by maxburns090 edited

To automate the changing of ownership of files and folders in Windows 10, you can make use of PowerShell scripts. PowerShell is a powerful scripting language that allows you to automate various tasks in Windows. Here's an example PowerShell script that you can use to change the ownership of a file or folder.
# Define the source and destination paths
$sourcePath = "C:\Path\to\source\file.txt"
$destinationPath = "C:\Path\to\destination\file.txt"

# Get the current owner of the source file
$sourceOwner = (Get-Item $sourcePath).GetAccessControl().Owner

# Set the owner of the destination file
(Get-Item $destinationPath).SetAccessControl((New-Object System.Security.AccessControl.FileSecurity).SetOwner($sourceOwner))

n this script, you specify the source path (the file or folder that has the desired ownership) and the destination path (the location where the file or folder is copied to). The script retrieves the current owner of the source file and sets it as the owner of the destination file. You can modify the paths in the script to match your specific source and destination locations. Save the script with a .ps1 extension (e.g., change-ownership.ps1), and then you can run it by executing powershell.exe and passing the script file as an argument (e.g., powershell.exe -ExecutionPolicy Bypass -File "C:\Path\to\change-ownership.ps1"). By utilizing such a script, you can automate the process of changing ownership when copying files or folders from one user to another, eliminating the need for manual changes.

This discussion is closed to new replies.