#PSTip How to get only files – the PowerShell 3.0 way!
The traditional or PowerShell 2.0 way of retrieving only files is:
Get-ChildItem $env:windir | Where-Object { ! $_.PSIsContainer }
The new parameter -File of the Get-ChildItem cmdlet simplifies the filtering:
Get-ChildItem $env:windir -File
Another way to achieve this in PowerShell 3.0 is another new parameter of Get-ChildItem cmdlet, –Attributes. It can be used to not only make this process easier but also speed up the whole process when executing against a large folder! This parameter takes the file system attributes as arguments and it also lets a way to combine with logical operators to achieve what we want. Let us see how:
Get-ChildItem $env:windir -Recurse -Attributes !D
In the above command, ‘!D’ specifies that we want to retrieve only items that are not directories! Simple and fast. Isn’t it?
Share on: