none
File and Folder Ownership and Permissions RRS feed

  • Question

  • I know there is a question that almost addresses my issue. 

    I will post the script that works great for folders and subfolders, I need it to do the files as well, any help?:

    -Jason
    Monday, October 7, 2019 3:47 PM

All replies

  • Part one of the script:

    $RootFolder = 'D:\one' $AdminGroup1 = 'PSY-NAS-Admins' $TotalModified = 0 $ActiveCount = 0 $TimeStamp = Get-Date -Format yyy-mm-dd-hhmm $LogName = "$TimeStamp.txt" $ActiveFolders = New-Object System.Collections.ArrayList #Functions function Set-Folders { [CmdletBinding()] param ([Parameter(Mandatory = $True, ValueFromPipeline = $True)] [string[]]$FolderLocation) begin { } process { $LocatedFolders = (Get-ChildItem -Path $FolderLocation -Directory -Recurse -ErrorAction SilentlyContinue).FullName $LocatedFolders.Count | Out-File -FilePath $LogName -Append foreach ($Folder in $LocatedFolders) { if ($ActiveFolders -notcontains $Folder) { $ActiveFolders.Add($Folder) | Out-Null } } } } function Set-Permissions { [CmdletBinding()] param ([Parameter(Mandatory = $True, ValueFromPipeline = $True)] [string[]]$BadFolder) begin { } process { &takeown /F $BadFolder | Out-Null $ACL = Get-Acl $BadFolder $ACL_Rule = new-object System.Security.AccessControl.FileSystemAccessRule ($AdminGroup1, "FullControl", "Allow") $ACL.SetAccessRule($ACL_Rule) Set-Acl -Path $BadFolder -AclObject $ACL | Out-Null } } #endregion Functions

    Monday, October 7, 2019 3:52 PM
  • Part two of the the script:

    #Populate first round of folders to array
    Set-Folders -FolderLocation $RootFolder
    
    #Begin the dirty
    for ($i = $ActiveFolders.Count; $i -ge 0; $i--) {
    	$MyFolder = $ActiveFolders | Select-Object -first 1
    	#If current array value is not blank
    	if ($MyFolder -ne $null) {
    		#Append current folder to log
    		$MyFolder | Out-File -FilePath $LogName -Append
    		#Take ownership & set permissions
    		Set-Permissions -BadFolder $MyFolder
    		#Find folders within after updating permissions
    		Set-Folders -FolderLocation $MyFolder
    		#Remove folder from array
    		$ActiveFolders.Remove($MyFolder)
    		#Up count of modified folders
    		$TotalModified++
    		#Update progress bar
    		Write-Progress -Activity "Current Folder Count: $i  Total Modified: $TotalModified" -Status $MyFolder -PercentComplete (100)
    		$i = $ActiveFolders.Count
    	}
    }
    Write-Host "Processed $TotalModified folders.  Operation complete." -ForegroundColor Red -BackgroundColor Green

    Monday, October 7, 2019 3:54 PM
  • Hi Jstearns421,

    In order to help you better, i suggest you discuss with script expterts over here:

    https://social.technet.microsoft.com/Forums/en-US/home?forum=winserverpowershell

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us.

    Thank you for your understanding.

    Regards,

    Daniel


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, October 8, 2019 1:46 AM
    Moderator
  • Hi,
    Just want to confirm the current situations.
    Please feel free to let us know if you need further assistance.
    Best Regards,
    Daniel

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, October 10, 2019 2:04 AM
    Moderator
  • Hi,
    This is Daniel and wish you all the best!
    As this thread has been quiet for a while, we will mark it as ‘ Propose answered’ as the information provided should be helpful. If you need further help, please feel free to reply this post directly so we will be notified to follow it up. You can also choose to unmark the propose answer as you wish.
    BTW, we’d love to hear your feedback about the solution. By sharing your experience you can help other community members facing similar problems. Thanks for your understanding and efforts.
    Best Regards,
    Daniel

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, October 11, 2019 8:11 AM
    Moderator