Excluding folders from scanning with Get-ChildItem

Gods of PowerShell

I, your humble n00b, request guidance on the ways of Powershell

Hi all, so, i have to remove files permissions from a shared directory if the file has the LastWriteTime attribute as 30 days. This is the script i wrote:

$pathFiles = "Dir_A" #Root path for searching for files $ExcludeScan = "Dir_B*.*" #Exclude files and folders $sd=get-childitem -File -Path $pathFiles -Recurse | where-object {$_.LastWriteTime -lt (get-date).AddDays(-30) -and $_.FullName -notlike $ExcludeScan} foreach($file in $sd) { $file | remove_permissions #Not actual syntax } 

I have defined the root path for scanning, and also a second path that needs to be excluded for whatever happens in the foreach loop.

$pathFiles = The root of the folder structure to scan $ExcludeFiles = folder to be excluded from the scan $sd = the command to scan the path based on LastWriteTiime attribute AND excluding a certain folder 

Now, if i need to exclude just one folder, that work as a charm. And for the past couple of days i have done just that. Problem is that i can’t define more than one folder to exclude. If i add one more with the structure as follows:

$pathFiles = "Dir_A" #Root path for searching for files $ExcludeScan = "Dir_B*.*" #Exclude files and folders $ExcludeScan = "Dir_C*.*" #Exclude files and folders $sd=get-childitem -File -Path $ScanPath -Recurse | where-object {$_.LastWriteTime -lt (get-date).AddDays(-30) -and $_.FullName -notlike ($ExcludeScan1 -or $ExcludeScan2)} 

this breaks the whole exclusion process and all files have their permissions.

I tried also using

get-ChildItem -Exclude 

to define several folders, but to no avail.

Anyone can help?

submitted by /u/OpenScore
[link] [comments]

Leave a Reply