Friday 30 August 2019

powershell - What is the correct syntax to exclude a folder's contents but not the folder itself


I have a powershell script that copies files from $source to $dest and it excludes some files types. It excludes my csv files and web.config files fine, but it won't exclude the Log folder contents. This is my script, what is the correct syntax to exclude the files contents but not the Log folder itself?


$exclude = @('Thumbs.db','*-Log.csv','web.config','Logs/*')

Get-ChildItem $source -Recurse -Exclude $exclude | Copy-Item -Destination {Join-Path $dest $_.FullName.Substring($source.length)}

Answer



You'll need to setup a second Array with the paths you want to avoid and then add a filter in the pipe. Here's some sample code:


$exclude = @('Thumbs.db','*-Log.csv','web.config','Logs/*')
$directory = @("C:\logs")
Get-ChildItem $source -Recurse -Exclude $exclude | where {$directory -notcontains $_.DirectoryName}

No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...