Root Loop
Root Loop

Reputation: 3162

powershell set variable then pass to filter?

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!

Upvotes: 0

Views: 307

Answers (2)

nimizen
nimizen

Reputation: 3419

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

Upvotes: 0

Kohlbrr
Kohlbrr

Reputation: 4069

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'} | ...

Upvotes: 2

Related Questions