Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude list of items from Get-ChildItem result in powershell?

Tags:

powershell

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 ?#> } 

?

like image 908
Tar Avatar asked Oct 06 '13 10:10

Tar


People also ask

How do I Get-ChildItem to exclude folders?

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.

How do I get a list of files in a directory in PowerShell?

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.

What does Get-ChildItem do in PowerShell?

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.


1 Answers

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.

like image 68
js2010 Avatar answered Sep 24 '22 12:09

js2010