Link to home
Start Free TrialLog in
Avatar of rookie_b
rookie_bFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Optimize a PowerShell Script to get last edit date

I use PowerShell to check last edit date on files in folder, but it can take a while to scan a folder sometimes. I was wondering if there was a way to run the task in parallel streams using jobs or something like that. Is that possible?


$path = \\server\share
Get-ChildItem -File -Recurse -Force -Path $path |select fullname, lastwritetime |sort lastwritetime -Descending |select -First 20 |Ft

Open in new window

Avatar of ste5an
ste5an
Flag of Germany image

Is it possible? Yes.

But: It will not really speed up things, when the FS is the bottleneck, cause the FS access is sequential.
Avatar of rookie_b

ASKER

Thanks ste5an. OK, so I am guessing it might make sense only if you are doing multiple paths at the same time, so say all top level folders on a volume? Would that make more sense for something like this, or is that still irrelevant?
I tried that and it seems slower than do it sequential
If you insist you can try this.
The reason why is each folder might have different number of last descending time
I don't know what you attempt to achieve so this is the best case.

## Note: assume you have powershell v7.

$path = \\server\share
$dirs = Get-ChildItem -Path $path -directory

$dirs | ForEach-Object -Parallel {Get-ChildItem -File -Recurse -Force -Path $_ |select fullname, lastwritetime |sort lastwritetime -Descending |select -First 20;} | select fullname, lastwritetime |sort lastwritetime -Descending |select -First 20


When these paths are on different FS, then yes, you can get some reduce in in running time. But as long as it is the same FS, then no.
It also depends on the type of FS and number of files and folders involved.

You need to test it.

(based on my past experience in the past with NTFS, DFS and EXT4 shares.)
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial