![]() Now consider what happens when applying a similar filter to exclude a directory, e.g. These examples also assume the use of the -Recurse parameter to grab all files and directories under the current directory (click for enlarged image). Also, the output shown here is a pretty-printed version of output from Get-ChildItem that illustrates the relevant points more clearly than the raw output of the cmdlet. By using the standard -Exclude parameter with an argument of *.user, you can filter out two files in this example the left side shows the unfiltered result and highlights the items that are filtered out on the right side when the -Exclude parameter is applied. ![]() Revealing the ProblemĬonsider first excluding files from a directory listing. Please refer to the Get-EnhancedChildItem API for the details of how to use it, and use the download page to get the code. I’ll start by revealing the not very surprising ending: the new cmdlet I’ve written, Get-EnhancedChildItem, extends the capabilities of Get-ChildItem to include, among others, the -ExcludeTree parameter. This provides you what you need to be immediately productive, so that you can then stop reading as soon as you have run out of time or interest in understanding everything behind it. With this article I am going to reverse my usual approach by starting with the practical application and then work backwards towards the theory. Most importantly, I need to be able to exclude entire subtrees, hence the topic of this article. My wish-list included being able to filter out those files that were not under source control, which is a feature I use extensively in my series of articles on Subversion. But for me, Get-ChildItem still seemed to be missing some crucial functionality. This version adds a few new parameters: -Directory to exclude files -File to exclude directories -Attributes to filter by selected attributes -Hidden, -ReadOnly, and -System to filter by those particular attributes. At the time of writing, PowerShell V3 is nearing release. These options provide the flexibility to get a lot out of this cmdlet, even though they seem few in number. Get-ChildItem provides options that let you clamber recursively through a directory tree, include or exclude system items, or filter your output with included or excluded targets. But that extraordinary design capability is the basis of another story. The documentation summary simply states “Gets the items and child items in one or more specified locations.” There is a reason that it does not say files and folders : In PowerShell, locations may be folders, and items may be files, but unlike Linux or DOS, a location in PowerShell may also be in your registry, GAC, variables, environment or elsewhere. Get-ChildItem is analogous to ls in Unix/Linux or dir in DOS in that it allows you to view the contents of your filesystem. The Get-ChildItem cmdlet is arguably the fundamental workhorse of PowerShell. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |