Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

PowerShell Script - Recursively Set / Replace Owner on all Files and Folders for a Given Path

$
0
0

I just finished my first Powershell Script and thought I'd share for peer review and / or to help others out there trying to do the same thing. Let me know if you have any comments or suggestions on how I could have done this better.

Is there a more suitable property to output the path without the Microsoft.PowerShell.Core prefix?

Write-Host "Owner Set -" $_.Path
Returns:
Owner Set - Microsoft.PowerShell.Core\FileSystem::\\COMPUTERNAME\C$\FolderName\FolderA

I'd also like any advice on adding error-handling /output to the script.

 #PowerShell Script - Recursively Set / Replace Owner on all Files and Folders for a Given Path

$Path = '\\COMPUTERNAME\C$\FolderName'
$ntAccount = New-Object System.Security.Principal.NTAccount("Administrators")

Get-ChildItem -LiteralPath $Path -Force -Recurse -ErrorAction SilentlyContinue | Get-Acl |
ForEach-Object {  
 $_.SetOwner($ntAccount)
 Set-Acl -Path $_.PSPath $_
 Write-Host "Owner Set -" $_.Path
 }


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>