Match strings stored in variables using PowerShell

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

if I run the following:

Following PowerShell Basics: Conditional Operators -Match -Like -Contains & -In -NotIn:

This returns true.

Answer:

I’d say use wilcards and the like operator, it can save you a lot of head aches:

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:

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:

Source:

Match strings stored in variables using PowerShell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply