Last active
March 6, 2022 17:00
-
-
Save Mossman1215/61b61f25eb5da5cba82ab4829302e376 to your computer and use it in GitHub Desktop.
Change ownership powershell snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#References | |
#base script | |
#https://stackoverflow.com/questions/22988384/powershell-change-owner-of-files-and-folders | |
#parameters | |
#https://powershell.org/forums/topic/file-path-parameter/ | |
param ( | |
[Parameter(Mandatory=$true)] | |
[ValidateScript({Test-Path -Path $_})] | |
[string]$path | |
) | |
# Define the owner account/group | |
$loggedInUser = "{0}\{1}" -f $env:COMPUTERNAME, $env:USERNAME | |
$Account = New-Object -TypeName System.Security.Principal.NTAccount -ArgumentList $loggedInUser; | |
# Get a list of folders and files | |
$ItemList = Get-ChildItem -Path D:\games -Recurse; | |
# Iterate over files/folders | |
foreach ($Item in $ItemList) { | |
Write-Verbose -Message $Item | |
$Acl = $null; # Reset the $Acl variable to $null | |
$Acl = Get-Acl -Path $Item.FullName; # Get the ACL from the item | |
$Acl.SetOwner($Account); # Update the in-memory ACL | |
Set-Acl -Path $Item.FullName -AclObject $Acl; # Set the updated ACL on the target item | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment