Question:
I am attempting to create a backup script that will move files that are older that 30 days, but I want to be able to exclude folders from the list
1 2 3 |
$a = "C:\\Temp\\Exclude\\test" $b = "C:\\Temp\\Exclude" |
if I run the following:
1 2 |
$a -match $b |
Following PowerShell Basics: Conditional Operators -Match -Like -Contains & -In -NotIn:
1 2 3 |
$Guy ="Guy Thomas 1949" $Guy -match "Th" |
This returns true
.
Answer:
I’d say use wilcards and the like operator, it can save you a lot of head aches:
1 2 |
$a -like "$b*" |
The match operator is using regex pattern and the path is having regex special characters in it (the escape characeter). If you still want to use -match – make sure to escape the string:
1 2 |
$a -match [regex]::escape($b) |
This will work but keep in mind that it can match in the middle of the string, you can add the ‘^’ anchor to tell the regex engine to match from the begining of the string:
1 2 |
$a -match ("^"+[regex]::escape($b)) |