I want to get list of files (actually number of files) in a path, recursively, excluding certain types:
Get-ChildItem -Path $path -Recurse | ? { $_.Name -notlike "*.cs" -and $_.Name -notlike "*.tt" }
but I have a long list of exclusions (to name a few):
@("*.cs", "*.tt", "*.xaml", "*.csproj", "*.sln", "*.xml", "*.cmd", "*.txt")
How to get the list using this form:
Get-ChildItem -Path $path -Recurse | ? { <# what to put here ?#> }
?
To exclude directories, use the File parameter and omit the Directory parameter, or use the Attributes parameter. To get directories, use the Directory parameter, its "ad" alias, or the Directory attribute of the Attributes parameter.
If you want to list files and directories of a specific directory, utilize the “-Path” parameter in the “Get-ChildItem” command. This option will help PowerShell list all the child items of the specified directory. The “-Path” parameter is also utilized to set the paths of one or more locations of files.
The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers and use the Depth parameter to limit the number of levels to recurse.
This works too:
get-childitem $path -recurse -exclude *.cs,*.tt,*.xaml,*.csproj,*.sln,*.xml,*.cmd,*.txt
Note that -include only works with -recurse or a wildcard in the path. (actually it works all the time in 6.1 pre 2)
Also note that using both -exclude and -filter will not list anything, without -recurse or a wildcard in the path.
-include and -literalpath also seem problematic in PS 5.
There's also a bug with -include and -exclude with the path at the root "\", that displays nothing. In unix it gives an error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With