Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 295
Yong Cai Avatar asked Dec 12 '25 15:12

Yong Cai


1 Answers

-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\\'} 
like image 192
T-Me Avatar answered Dec 15 '25 18:12

T-Me



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!