Powershell Compare string to folders

I am completely new to Powershell and I want to know if a language code is contained on a folder list. I am using this little script.

set-executionpolicy remotesigned -Scope CurrentUser
$path = "C:\Projects\Eg_Proyect\2022_834"
$lang = "DE-DE"
$Lsta2 = Get-ChildItem -Path $path -Name
$Lsta2 
$Lsta2.Count
$Lsta2 -contains $lang

The result is:

0_SOURCE
10_EN-GB_PL
11_EN-GB_CS
12_EN-GB_DE-DE
13_EN-GB_TR
1_EN-GB_ES-ES
2__
3_EN-GB_IT-IT
4_EN-GB_IT-IT
5_EN-GB_HU
6_EN-GB_HU
7_EN-GB_NL-NL
8_EN-GB_NL-NL
9_EN-GB_PT-PT
14
**False**

Why it does not return True?


Solution 1:

There is always confusion about the string method .Contains() which looks to find a certain substring withing another string and the PowerShell -contains operator, which looks for a complete, exact (case-insensitive) item in an array.

Since in your case you have an array of strings, you could use:

[bool]($Lsta2 -match 'DE-DE')    # uses RegEx. returns True or False because of the cast to `[bool]`

OR

[bool]($Lsta2 -like '*DE-DE*')  # uses WildCards ('*' and/or '?')

OR use the .Contains() string operator on every string individually. This is of course a waste of time, AND it works Case-Sensitively, but to show that it can be done:

[bool]($Lsta2 | ForEach-Object { if ($_.Contains('DE-DE')) { $true }})

Solution 2:

You can use -match to search with a regex in a string

$Lsta2 -match $lang