Sort-Object

by aleqs at 2013-03-25 01:16:24

Hi!

I am running a script which uses the following command:

[code2=powershell]$FilesSource = Get-ChildItem -Path "D:\Test\Test" | Where-Object {!$.PsIsConatiner} | Sort-Object LastWriteTime | Select-Object -Last 100 | Select-Object FullName[/code2]

It is looping through some folders and each folder is containing about 70 000 files.

This uses alot of CPU, is there something I can do to make it use less cpu ? I think that the problem is Sort-Object.

Also, if I am excluding the Sort-Object and Select-Object, which files will it choose then ? Random ?

Thanks in advance.
by notarat at 2013-03-25 04:33:30
There are a few different ways you could slow it down some to conserve system resources.

1 - You could create a delay timer in between the commands
2 - You could use a function to set the processor affinity lower for powershell (Taken from Eric Weintraub’s Blog at: http://digitaljive.wordpress.com/2011/1 … owershell/)

$instances = Get-Process powershell_ise
foreach ($i in $instances) {
$i.ProcessorAffinity=15
}
by poshoholic at 2013-03-25 06:03:35
A few other ideas:

1. If you are using PowerShell 3, use Get-ChildItem -Path 'D:\Test\Test' -File instead of Get-ChildItem -Path 'D:\Test\Test' | Where-Object {!$
.PSIsContainer} so that you’re filtering results at a lower level and just getting back files in the first place. That’s not much of an efficiency improvement, but it’s always better to filter as early as possible.

2. It seems like you are looking for the 100 oldest files in a folder and then sorting them. If Sort-Object is slowing down the invocation too much or causing too many resources to be used, think about how you can reduce the number of objects being sorted before you sort them. For example, if you want the 100 oldest files, is it safe to exclude files that are less than 3 months old? 6 months? Some other filter? Because if so, you could change your Where-Object stage in the pipeline and filter out based on LastWriteTime before you sort the files. Think about changing your result set from the 100 oldest files to the 100 oldest files that are at least 6 months old (or however old you want them to be before you start caring about them in this case).
by aleqs at 2013-03-25 06:59:13
Thanks notarat, for the tip on how to set the processor affinity lower on processes, I dont think that I will use it in this script, though I might have use for it in the future :slight_smile:

poshoholic, I will change to "Get-ChildItem -Path ‘D:\Test\Test’ -File". And it was a great tip on Sort-Object.