Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have v1 of powershell on my servers. It would be easy if I had a newer version of PowerShell but that is not an option for me at this point.

I have a script that looks to see if a file in a specific folder is older than 10 min and if it is, then send out a note. The script works great if there are no sub directories or only a few directories but the more there is the way longer it takes.

I also can't just create a seperate folder to look in, that is also not an option.

Is there a way that I can run this line to make it so I only look at file and not directories? oh, and yes, i did run it without the -recurse but it didn't work.

function FileAge($path, $days, $hours, $mins)
    $files = @(get-childitem $path -include *.* -recurse | where {($_.psIsContainer -eq $false) -and ($_.LastWriteTime -lt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins))})

Without going into lots of detail, it does work if I have the path c:\temp .. it takes about 30 seconds but if I change the path to c:\ it takes 2 hours.

share|improve this question… – STTR Jun 19 '13 at 22:52
UPDATE I was not able to fix the issue but I did manage a work around. For now, this is working for me but if someone has an classy way to do it, please let me know. – Don Jun 20 '13 at 16:18
I guess I should add the rest of my coment. before the code ran I do a get-childitem $path *.txt | set-content $path temp.txt. Then I just put a foreach loop around it but only feed in my files. I will do a cleaner update in a bit, when I am not under a time issue. – Don Jun 20 '13 at 16:21

Command line:

ps-run.cmd listf15.ps1

first run ~ 33 GB/min (~30 min - 1Tb)

last run ~ 125-250 GB/min (~8-4 min - 1Tb)

it slow ...


@IF "%time:~0,1%" GTR "0" SET current=%DATE:~-4%-%DATE:~3,2%-%DATE:~0,2%@%TIME:~0,2%.%TIME:~3,2%.%TIME:~6,2% 
@IF "%time:~0,1%" LSS "0" SET current=%DATE:~-4%-%DATE:~3,2%-%DATE:~0,2%@0%TIME:~1,1%.%TIME:~3,2%.%TIME:~6,2%
@echo %current%                                                                                              
powershell .\%* 9>&1 8>&1 7>&1 6>&1 5>&1 4>&1 3>&1 2>&1 0>&1 >> .\err%current%.txt                           

::STDIN                    0                  Keyboard Input                                                 
::STDOUT                   1                  Output in the Command Prompt window                            
::STDERR                   2                  Output error in the command prompt window                      
::UNDEFINED                3-9                                                                               


$RunTime = [System.Diagnostics.Stopwatch]::StartNew()
echo "Run @ $CurentTime"

[string]$ReportLine = [char]183
$ReportLine = [string]$ReportLine * 80

$cd = [environment]::CurrentDirectory



ac -Path $logfile -Va $ReportLine
ac -Path $logfile -Va "Last Write Time $HH hours"
ac -Path $logfile -Va $TM" - "$CurentTime
ac -Path $logfile -Va $ReportLine

$Files = ls ${Env:SystemDrive} -recurse -force -filter "*" | ?{!$_.PSIsContainer -and ($_.LastWriteTime -GT [DateTime]$TM)}

$i =0
Foreach ($file in $Files) {
'"{0:yyyy-MM-dd HH:mm:ss}";"{1}";"{2}"' -f $file.LastWriteTime, $file.Length, $file.FullName | `
Out-File -encoding "Default" -append $logfile -width 400 -OutBuffer 2147483647 -EA 0


write-host "Count : $i"


ac -Path $logfile -Va $ReportLine
ac -Path $logfile -Va "Count : $i"

write-host "End @ $CurentTime"
write-host "Total Run : $($RunTime.Elapsed.ToString())"

ac -Path $logfile -Va $CurentTime
ac -Path $logfile -Va "Total Run : $($RunTime.Elapsed.ToString())"

ac -Path $logfile -Va $ReportLine

Output, SystemDrive.txt:

Last Write Time 10 hours
06/20/2013 16:48:04 - 06/21/2013 02:48:04
"2013-06-20 17:45:47";"0";"C:\Users\Administrator\Application Data\Macromedia\Flash Player\Logs\flashlog.txt"                                                
"2013-06-20 19:41:57";"602";"C:\Users\Administrator\Application Data\Macromedia\Flash Player\\support\flashplayer\sys\settings.sol"            
"2013-06-20 19:41:57";"81";"C:\Users\Administrator\Application Data\Macromedia\Flash Player\\support\flashplayer\sys\\settings.sol"
"2013-06-21 00:45:35";"372637";"C:\Users\Administrator\Application Data\Microsoft\CryptnetUrlCache\Content\018C4A5B5879D48381195FA49E2E6532"                 
"2013-06-20 18:10:34";"31235";"C:\Users\Administrator\Application Data\Microsoft\CryptnetUrlCache\Content\08A1C9C30288A186F932D2F6CE954534"                  

... etc ....
Count : 4333
21.06.2013 2:57:42
Total Run : 00:03:55.8185680

Output, err2013-06-21@02.48.04.txt:

Run @ 06/21/2013 02:48:04
Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
At D:\VBS\PS-03FileList\listf15.ps1:20 char:12
+ $Files = ls <<<<  ${Env:SystemDrive} -recurse -force -filter "*.*" | ?{!$_.PSIsContainer -and ($_.LastWriteTime -GT [DateTime]$TM)}
    + CategoryInfo          : ReadError: (C:\Not! David Blaine! раскукож меня обратно! ... VJCP 68096 97):String) [Get-ChildItem], PathTooLongException
    + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

%windir%\system32\WindowsPowerShell\v1.0\ powershell.exe.config, powershell_ise.exe.config:

<?xml version="1.0"?> 
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v2.0.50727"/> 
        <supportedRuntime version="v4.0.30319"/>

without error:

Command line, Powershell 64-bit not use .Net 4.0 permanently and not use multithreading:

reg add hklm\software\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 0 /f

Command line, Powershell 32-bit not use .Net 4.0 permanently and not use multithreading:

reg add hklm\software\wow6432node\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 0 /f
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .