Search code examples
powershellcomparison-operatorsget-childitem

Powershell unable to exact match folder full name


I have a script to get all the files inside the folder by using powershell get-childitem command. I am using the pipeline with match parameter to filter the file name as follows:

Get-ChildItem -Path \\path\*  -recurse | Where-Object {$_.FullName -match 'backup'} 

Output: enter image description here

Expectation Result:

Only folder name exact match 'backup' will be in result, backup 2 should be not in the result.

Seem like the match parameter will behave alike 'backup*', Tried others comparison operators such as -ceq, -eq, -like, -contains and etc, but none of them working as intended, most of them return no result at all. What's the comparison operators should I use or is there other solution for this issue?


Solution

  • -match uses regex and checks if a string cointains it (indeed like '*backup*' ). However the string you get from $_.fullname uses \ to seperate folders. So we could check for '\backup\'. But since -match uses regex we need to escape the \ with \:

    ...| Where-Object {$_.FullName -match '\\backup\\'}