Reputation: 1
I am just starting to use Powershell )
I need to compare the file name with a list of file names to exclude some files from further transfer .
This script I wrote :
$E_list= @("**a8e4022a-41c4-4480-b627-02fd6ca03413**","**042d7f01-46da-4f64-b0a8-1fa90e2386d8**")
Foreach($file in(Get-ChildItem $path))
{$sname=$file.Name.SubString(0,36)
if($sname -**notcontains** $E_list)
{write-output $file.name}
}
This is what I get :
042d7f01-46da-4f64-b0a8-1fa90e2386d8.20220807T043155.239-0500.2B2DAA77BB5DBC120EC7.log
042d7f01-46da-4f64-b0a8-1fa90e2386d8.20220807T053153.074-0500.7BBEE35804E5257E9322.log
042d7f01-46da-4f64-b0a8-1fa90e2386d8.20220807T063152.902-0500.CFE11DD033449E5C566C.log
042d7f01-46da-4f64-b0a8-1fa90e2386d8.20220807T073153.266-0500.62C3B67C51A3298D7E41.log
514e18c3-73ec-4203-bd6b-b7088f35f86a.20220807T011525.607-0500.28EC734139367578396D.log
77414eee-054c-4251-b564-d7dc8f18c074.20220807T012552.070-0500.98E353CC0CB50F1901C2.log
77414eee-054c-4251-b564-d7dc8f18c074.20220807T022335.696-0500.570D615179EC19BA5454.log
77414eee-054c-4251-b564-d7dc8f18c074.20220807T023742.213-0500.42759D279D05B3F5476F.log
77414eee-054c-4251-b564-d7dc8f18c074.20220807T042724.518-0500.340A0A2454D6C72CFDEA.log
a8e4022a-41c4-4480-b627-02fd6ca03413.20220807T073747.827-0500.B7411E2EF3038A32BF17.log
a8e4022a-41c4-4480-b627-02fd6ca03413.20220807T074255.366-0500.CE5DB99320C88F4CA300.log
a8e4022a-41c4-4480-b627-02fd6ca03413.20220807T074756.774-0500.7F5A11C5C8B6C9FF5E68.log
a8e4022a-41c4-4480-b627-02fd6ca03413.20220807T075255.354-0500.C460A6E0122A9DD787E6.log
bb57d78f-9bfa-41c0-9a78-f4709a7e02ba.20220807T012609.428-0500.942CD4B421572B911155.log
d80daa0d-124b-4061-86b8-d76e5c02d48a.20220807T011529.679-0500.A13D8D9EE5A5628F4C02.log
d80daa0d-124b-4061-86b8-d76e5c02d48a.20220807T012603.506-0500.4BA46E891A4DDA1D55B3.log
d80daa0d-124b-4061-86b8-d76e5c02d48a.20220807T023744.215-0500.3D5F1792A1FEDA5C2AAB.log
d80daa0d-124b-4061-86b8-d76e5c02d48a.20220807T040642.210-0500.637614FAEF716E613EC7.log
dbe57367-16c9-4115-ad75-0f33449b56e4.20220807T043150.243-0500.FE8A3AF63EDF427D33FC.log
Basically all files in this folder.
I expect files marked bold not to appear in the output. but somehow they are.
I will appreciate your help.
Upvotes: 0
Views: 72
Reputation: 27756
You can use the -Exclude
parameter of Get-ChildItem
to greatly simplify and fix the code:
$E_list= 'a8e4022a-41c4-4480-b627-02fd6ca03413*', '042d7f01-46da-4f64-b0a8-1fa90e2386d8*'
Get-ChildItem $path\* -Exclude $E_list | ForEach-Object { $_.Name }
As for what you have tried:
-notcontains
- i.e. $E_list -notcontains $name
. Alternatively, use the -notin
operator, i.e. $name -notin $E_list
.*
in the $E_list
strings and around -notcontains
don't do any good. The -contains
and -notcontains
operators don't support wildcard matching. For that purpose you'd have to use the -like
or -notlike
operators. The -Exclude
parameter of Get-ChildItem
also supports wildcards.Upvotes: 1