Search code examples
powershellget-childitem

Using PowerShell Where-Object not working with Get-ChildItem


I am trying to get directories that contain "COO", but after adding the Where-Object nothing is returned, the script is looping through each of the folders but nothing makes it to the file when the Where-Object is in place.

This works:

Get-ChildItem -Path $path -Recurse -Directory | select Fullname | Export-csv c:users\shay\desktop\test.csv

This doesn't:

Get-ChildItem -Path $path -Recurse -Directory | Select Fullname | Where-Object {$_.Fullname -like 'COO'} | Export-Csv c:users\shay\desktop\test.csv

Solution

  • In order to use the -Like parameter you'll need to include a wildcard (*), try the following:

    Get-ChildItem -Path $path -Recurse -Directory | select Fullname | where-object {$_.Fullname -like '*COO*'} | Export-csv c:users\shay\desktop\test.csv
    

    You could also potentially move the expression to the Get-ChildItem portion of this snippet.

    Get-ChildItem -Path $path -Recurse -Directory -Filter '*COO*' | select Fullname | Export-csv c:users\shay\desktop\test.csv