Match strings stored in variables with PowerShell

I am trying to create a backup script that will move files older than 30 days, but I want to be able to exclude folders from the list

$a = "C:\\Temp\\Exclude\\test"
$b = "C:\\Temp\\Exclude"

if I run the following:

$a -match $b

The following PowerShell basics: -Match -Like -Contains & -In -NotIn conditionals :

$Guy ="Guy Thomas 1949"
$Guy -match "Th"

It returns true.

+3
source share
1 answer

I would say use wilcards and the like, this can save you a lot of headaches:

$a -like "$b*"

, (escape-). -match - , :

$a -match [regex]::escape($b)

, , , '^', :

$a -match ("^"+[regex]::escape($b))
+6

All Articles