This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
  • #5719

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


    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: ... owershell/)

    $instances = Get-Process powershell_ise
    foreach ($i in $instances) {

    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 🙂

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

You must be logged in to reply to this topic.