question

tarouchabi-7271 avatar image
0 Votes"
tarouchabi-7271 asked tarouchabi-7271 commented

I want to move files to different folders for each permission with Powershell.

The files in the folder have different NTFS permissions.
I want to move files to different folders for each permission with Powershell or C#.
Do you have good code?

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered tarouchabi-7271 commented

Hi,

Please check to see if this works for you.

 $source = "C:\temp\source"
 $destination = "C:\temp\destination"
 $files = @()
 Get-ChildItem -Path $source -File -Recurse | ForEach-Object {
     $files += [pscustomobject]@{
         sddl = (Get-Acl $_.FullName).GetSecurityDescriptorSddlForm('All')
         path = $_.FullName
     }    
 }
 $folders = @{}
 $counter = 0
 $files |Select-Object -ExpandProperty sddl -Unique | ForEach-Object {
         $folders.add($_,"$destination\folder$counter")
         $counter ++
 }
 $files | ForEach-Object {
     if(-not (Test-Path -Path $folders.($_.sddl))){
         New-Item -Path $folders.($_.sddl) -ItemType Directory
     }
     Move-Item -Path $_.path -Destination $folders.($_.sddl)
 }

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It's Cool !!!!!!!!!!!!!!! Thanks alot^^

0 Votes 0 ·