Caligo Caligo - 3 months ago 11
PowerShell Question

deleting array content which matches with an other array

the goal of my script is to copy some files. i import a config file, which gives me the path and a filename for an exclude (the exclude shouldnt get copied).

now, i have 2 arrays, one for the folders and one for the excludes and i want to delete all rows from the array1 which contain the exclude file from array2.

i tried something like this but it has not worked...

$arrFolder = $arrFolder - $arrExcludes


or

$arrFolder | $_ -replace "$arrExcludes",""


i was searching for houres with no good solution. can anybody help me?

thank you & regards, mischa

Answer

You can also use Except from LINQ (PS 3.0+):

$arr1 = "Folder1", "Folder2", "Folder3", "Folder4"
$arr2 = "Folder2", "Folder3"

[System.Linq.Enumerable]::Except([string[]]$arr1, [string[]]$arr2)
Comments