Therefore, it is (probably) better to use -include rather than -filter.
-include
-include allows to specify an array of filename patterns that further removes items from matched files that are not matched by the filename patters.
-include only works in conjunction with at least one -recurse, -depth or -path.
Unlike -filter, -include is implemented by PowerShell.
Because -filter requires -path if -recurse and -depth are not specified, the the first of the following two commands does not return anything, but the second does (if there are *.txt files in the current directory):
List or exclude items with specific attributes (such as files, directories etc.)
The -attributes option allows to only list or excludeitems with a given set of attributes. The list of attributes that can be specified is that of System.IO.FileAttributes.
For example, to list only directories, the following would do
The fact that get-childItem returns specific objects turns out to be problematic if these objects are compared with comparison operators (such as -eq) to a string. The following probably won't do what the author expected to do:
foreach ($file in get-childItem *) {
if ($file -eq "readme.txt") {
...
}
}
In order to have get-childItem return an array of strings, it must be invoked with the -name argument.
Let get-childItem return strings with -name
When get-childItem is invoked with the -name parameter, the cmdLet returns an array of strings rather then specialized types for the child items.
This is especially useful to get a more readable (imho) output, especially when combined with -recurse (in which case it behaves like the good old cmd.exe command dir /b …).
Querying child items from another provider (such as the registry, environment variables etc.)
Usually, get-childItem is used to list files and directories. However, it can also list items from a non file system provider by prepending the path with the provider and a colon.
The following command lists all environment variables that contain user in their names:
get-childItem env:*user*
The previous command is of course just an abbreviation that omits the -path attribute and is equivalent to