How to add multiple expression in calculated property

Hi Team,

I wanted to add a multiple expression under Calculated property for powershell.

Get-ChildItem -Path "c:\temp" -Recurse |
Select-Object @{l='TXT';e={$_.Extension -eq '.txt'}},FullName

Output looks like:

TXT FullName


False C:\temp\temp.csv
False C:\temp\test.csv
True C:\temp\temp.txt
True C:\temp\test.txt
False C:\temp\test1.docx

The above output returns True only to ‘.txt’ file. If I want to add an additional expression to make ‘.docx’ also to TRUE, how to achieve this ?

I found the result.

" "
Get-ChildItem -Path “c:\temp” -Recurse |
Select-Object @{l=‘TXT’;e={$_.Extension -in (’.txt’,’.docx’}},FullName
" "

You can put almost any valid expression into the script block of a calculated property. Another way could have been this:

Get-ChildItem -Path "c:\temp" -Recurse |
Select-Object -Property @{Name = 'TXT'; Expression = {$_.Extension -eq '.txt' -or $_.Extension -eq '.docx' }}, FullName

Thank you for this response Olaf.

@Win - I noticed that you had specified -Recuse so I used the Attributes parameter to exclude child directories from appearing as items in the result set (i.e., recursively return only files). I also introduced regex with the match operator and added a sort operation for the output.

Demo Code

Broken into 3 lines for legibility.

Get-ChildItem -Attributes !Directory -Path "A:\temp" -Recurse `
| Select-Object @{l='TXT';e={$_.Extension -match "\.(txt|docx?|\bmd\b)"}},FullName `
| Sort-Object TXT -desc;

Matches

  • .txt or
  • .doc and .docx or
  • .md (whole word only)

The whole word match (denoted with \b) is required for this operation because the Access file .mdb would be a true match otherwise.

File structure

Results

Thanks @dicey. Excellent