Win
September 9, 2021, 6:55pm
1
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 ?
Win
September 9, 2021, 7:55pm
2
I found the result.
" "
Get-ChildItem -Path “c:\temp” -Recurse |
Select-Object @{l=‘TXT’;e={$_.Extension -in (‘.txt’,‘.docx’}},FullName
" "
Olaf
September 9, 2021, 8:14pm
3
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
Win
September 9, 2021, 10:11pm
4
Thank you for this response Olaf.
dicey
September 11, 2021, 5:55am
5
@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