1

I'm pretty sure I have some synthax error in here, but i can't find it.

I wrote this little script here to find some Files in a Folder - I want all the Filepaths that don't have some of the terms in the $excludeList in there but my Contains-Function always returns $true. And when I enable the Write-Output-Line, it also, always sais $true

$path = 'C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications'
$excludeList = @('Test', 'Samples', 'BaseApp\Source', 'Application\Source', 'system application\source')

function Contains([string]$Fullpath, $ExcludeList) {
    $result = $false
    #Write-Output $ExcludeList
    $ExcludeList | % { $result = ($FullPath.Contains($_) -or $result) }
    return $result
}

(Get-ChildItem $path -Filter '*.app' -Recurse).FullName | % {
    if (!(Contains($_, $excludeList))) { $_ }
}

Here is the List of Files I'm trying to filter:

C:\WINDOWS\system32> (Get-ChildItem $path -Filter '*.app' -Recurse).FullName 
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\AMCBanking365Fundamentals\Source\Microsoft_AMC Banking 365 Fundamentals.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\AMCBanking365Fundamentals\Test\Microsoft_AMC Banking 365 Fundamentals Test Automations.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\APIV1\Source\Microsoft__Exclude_APIV1_.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\APIV1\Test\Microsoft__Exclude_APIV1_ Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\APIV2\Source\Microsoft__Exclude_APIV2_.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\APIV2\Test\Microsoft__Exclude_APIV2_ Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Application\Source\Microsoft_Application.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Base Application.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Czech language (Czechia).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Danish language (Denmark).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Dutch language (Belgium).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Dutch language (Netherlands).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_English language (Australia).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_English language (Canada).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_English language (New Zealand).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_English language (United Kingdom).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_English language (United States).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Finnish language (Finland).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_French language (Belgium).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_French language (Canada).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_French language (France).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_French language (Switzerland).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_German language (Austria).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_German language (Germany).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_German language (Switzerland).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Icelandic language (Iceland).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Italian language (Italy).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Italian language (Switzerland).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Norwegian language (Norway).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Russian language (Russia).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Spanish language (Mexico).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Spanish language (Spain).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Source\Microsoft_Swedish language (Sweden).app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Library-NoTransactions.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Bank.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Cash Flow.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Cost Accounting.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-CRM integration.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Data Exchange.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Dimension.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-ERM.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Fixed Asset.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-General Journal.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Graph.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Integration.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Invoicing.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Job.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Local.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Marketing.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Misc.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Monitor Sensitive Fields.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Permissions.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Physical Inventory.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Prepayment.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Rapid Start.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Report.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Resource.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Reverse.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-SCM.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-SINGLESERVER.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-SMB.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-SMTP.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-TestLibraries.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Upgrade.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-User.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-VAT.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\BaseApp\Test\Microsoft_Tests-Workflow.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\ClientAddIns\Source\Microsoft__Exclude_ClientAddIns_.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\CompanyHub\Source\Microsoft_Company Hub.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\CompanyHub\Test\Microsoft_Company Hub Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\DataArchive\app\Microsoft_Data Archive.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\DataArchive\test\Microsoft_Data Archive Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\datacorrectionfa\source\Microsoft_Troubleshoot FA Ledger Entries.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Email - Current User Connector\Source\Microsoft_Email - Current User Connector.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Email - Current User Connector\Test\Microsoft_Email - Current User Connector Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Email - Microsoft 365 Connector\Source\Microsoft_Email - Microsoft 365 Connector.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Email - Microsoft 365 Connector\Test\Microsoft_Email - Microsoft 365 Connector Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Email - Outlook REST API\Source\Microsoft_Email - Outlook REST API.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Email - Outlook REST API\Test\Microsoft_Library Outlook REST API.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Email - SMTP Connector\Source\Microsoft_Email - SMTP Connector.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Email - SMTP Connector\Test\Microsoft_Email - SMTP Connector Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\EssentialBusinessHeadlines\Source\Microsoft_Essential Business Headlines.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\EssentialBusinessHeadlines\Test\Microsoft_Essential Business Headlines Test.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\LatePaymentPredictor\Source\Microsoft_Late Payment Prediction.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\LatePaymentPredictor\Test\Microsoft_Late Payment Prediction Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\microsoftuniversalprint\source\Microsoft_Universal Print Integration.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\onprem permissions\source\Microsoft_OnPrem Permissions.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\onprem permissions\test\Microsoft_OnPrem Permissions Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\Onprem Permissions DACH\Source\Microsoft_OnPrem Permissions DACH.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\paypalpaymentsstandard\source\Microsoft_PayPal Payments Standard.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\paypalpaymentsstandard\test\Microsoft_PayPal Payments Standard Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\recommendedapps\source\Microsoft_Recommended Apps.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\recommendedapps\test\Microsoft_Recommended Apps Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\salesandinventoryforecast\source\Microsoft_Sales and Inventory Forecast.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\salesandinventoryforecast\test\Microsoft_Sales and Inventory Forecast Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\sendtoemailprinter\source\Microsoft_Send To Email Printer.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\simplifiedbankstatementimport\source\Microsoft_Simplified Bank Statement Import.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\SwissQRBill\Source\Microsoft_QR-Bill Management for Switzerland.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\SwissQRBill\Test\Microsoft_QR-Bill Management for Switzerland Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\system application\source\Microsoft_System Application.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\system application\test\Microsoft_System Application Test Library.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\system application\test\Microsoft_System Application Test.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\testframework\performancetoolkit\Microsoft_Performance Toolkit Samples.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\testframework\performancetoolkit\Microsoft_Performance Toolkit Tests.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\testframework\performancetoolkit\Microsoft_Performance Toolkit.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\testframework\TestLibraries\Any\Microsoft_Any.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\testframework\TestLibraries\Assert\Microsoft_Library Assert.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\testframework\TestLibraries\permissions mock\Microsoft_Permissions Mock.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\testframework\TestLibraries\Variable Storage\Microsoft_Library Variable Storage.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\testframework\TestRunner\Microsoft_Test Runner.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\vatgroupmanagement\source\Microsoft_VAT Group Management.app
C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications\vatgroupmanagement\test\Microsoft_VAT Group Management Tests.app

Please tell me what went wrong, and if you know a way to achieve the same result more smoothly, I'm open for suggestions :)

Thank you in advance.

Stefan Lippeck
  • 381
  • 2
  • 17
  • 2
    Classic [PowerShell Gotcha `#2`](https://stackoverflow.com/a/69644807/1701026), lose the parenthesis and comma in the function call: `if (!(Contains $_ $excludeList)) { $_ }` – iRon Feb 03 '22 at 07:40

2 Answers2

1

you need to remove parenthesis and comma in - if (!(Contains($_, $excludeList))) { $_ }

you have a problem with passing argument to a function. look here: How do I pass multiple parameters into a function in PowerShell?

$path = 'C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications'
$excludeList = @('Test', 'Samples', 'BaseApp\Source', 'Application\Source', 'system application\source')

function Contains([string]$Fullpath, $ExcludeList) {
    $result = $false
    #Write-Output $ExcludeList
    $ExcludeList | % { $result = ($FullPath.Contains($_) -or $result) }
    return $result
}

(Get-ChildItem $path -Filter '*.app' -Recurse).FullName | % {
    if (!(Contains $_ $excludeList)) { $_ }
}
Tal Folkman
  • 2,368
  • 1
  • 7
  • 21
  • Please, add some value to [your answers](https://stackoverflow.com/search?q=user%3A17674849+%5BPowerShell%5D) so that it has at least some more contents than a **comment** (from someone else). See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – iRon Feb 03 '22 at 08:37
  • thank you, I just wanted to come back here, because i figured it out myself, but you were faster =) – Stefan Lippeck Feb 03 '22 at 09:28
1
$path = 'C:\bcartifacts.cache\onprem\19.2.32968.33504\ch\Applications'    
[string[]]$excludeList = @('Test', 'Samples', 'BaseApp\\Source', 'Application\\Source', 'system application\\source')    
[string]$excludeListRegex = $excludeList -join('|')    
(Get-ChildItem $path -Filter '*.app' -Recurse).FullName | % {
    if ($_ -notmatch $excludeListRegex) { $_ }
}

This is a trick I've used in the past. Basically you are dynamically creating one big regex query and using that to match the lines. Downside is you must format the entries in the ExcludeList array in a regex friendly manner. You'll notice I escaped all the backslashes with backslashes. Backslash IS the escape char in regex so if you just want an actual backslash you have to type two backslahes like I did. The upside is this is really fast because the alternative is nested loops where you'd have to iterate through every file in the list once for every exclusion. Another downside is that i'm sure there's a limit to how long that regex query can get (not sure offhand what it is) but it's not infinitely scalable.

I believe there may be a more elegant way to code this but I'd have to put a lot more thought into it. ;-)

mOjO
  • 98
  • 6