I'm working on a Powershell script to compare directories, and I want to clean up some of the code if possible. The script as a whole works exactly like I want it to, but I feel like the code I've included below (which is only a small part of it) could be written better.
For the code below, it focuses on getting exclusions from a csv file and incorporating them into Get-ChildItem. I've found that you can't pull the excluded paths from the csv the same way as you can the files (since paths deal with FullName vs Name, plus have wildcards for folders and servers incorporated).
So what I have below works for me. But is there a way to make the Where-Object part that deals with excluding paths into a function since I have to call it twice? I've tried just making it into a function and tacking it on at the end, but that doesn't work. I've also tried putting it at the beginning as a function, but that didn't work either. And I know when you deal with functions and piping data, you have to set it up a specific way. So maybe I'm just doing something wrong. Anyways, if you have a suggestion for how to clean this up or make it more efficient, I'm open to seeing what you have.
$ExcludedPaths = @(Import-Csv -LiteralPath 'D:\ExclusionList.csv') |Select-Object -Expand ExcludedPaths
$ExcludedFiles = @(Import-Csv -LiteralPath 'D:\ExclusionList.csv') |Select-Object -Expand ExcludedFiles
$SourceFiles = Get-ChildItem -Recurse -Path $SourceDir -Exclude $ExcludedFiles -Force | Where-Object {
$FullName = $_.FullName
-not($ExcludedPaths|Where-Object {
$FullName -like "$_*"
})
}
$DestFiles = Get-ChildItem -Recurse -Path $DestDir -Exclude $ExcludedFiles -Force | Where-Object {
$FullName = $_.FullName
-not($ExcludedPaths|Where-Object {
$FullName -like "$_*"
})
}