0

I need to filter 30 types of file from 2T of data, i want to set a variable for get-childitem then pass to -filter for different type of files, but it doesn't work.....any idea why? The idea was if I use get-childitem 30 times it will slow down the system, so I only want to do it once and set the output as a variable and use it for filtering different types of files.

$a = Get-ChildItem -Recurse c:\work 
$a -filter .prt | .............  

Any suggestion please!

1
  • I'll suggest you limit your questions to one specific question at a time. Throwing a laundry list of questions into one thread results in questions that don't match the title and makes it hard for others to search for them later, and difficult for people to give you a comprehensive answer that covers all of them. Commented Mar 27, 2014 at 19:51

2 Answers 2

2

You can use Where-Object and filter off of the Name parameter. You can't use -filter on a variable.

Also, you need a wildcard to filter all files ending with ".prt" (if that's what you're trying to do).

$a = Get-ChildItem -Recurse c:\work
$a | Where-Object {$_.Name -like '*.prt'} | ...
Sign up to request clarification or add additional context in comments.

2 Comments

In my script i need to filter 30 types of file from 2T of data, if i add -filter to get-childitem, so I need to get-childitem for 30 times, it will slow down the system. The idea was to get result of get-childitem only once as a variable for later use. since the -filter can't be used on variables, maybe better just output get-childitem to txt file and search from there then. any better idea?
Realized the answer wasn't sufficient right after I posted it. It's been updated since to address the issue.
0

It is usually best to include only the data you're after rather than filtering it from a larger collection; consider using the -include parameter to achieve this. For example:

#Non-recursive
$fileTypes = @("*.prt","*.doc")
$a = Get-ChildItem c:\work\* -include $fileTypes

#Recursive
$fileTypes = @("*.prt","*.doc")
$a = Get-ChildItem c:\work\* -include $fileTypes -recurse

2 Comments

ThanKs for your answer, I tried your way, it took longer time to process than filtering each type of file separately and the results were not accurate after comparing results made by 2 ways
Yes, -Filter directly on Get-ChildItem is the fastest because it is the filesystem that does the filtering. But that filtering method is also simpler than what the cmdlets give you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.