CodexBloom - Programming Q&A Platform

PowerShell 7.3 - Trouble Filtering Large JSON Files with Where-Object and Select-Object

👀 Views: 0 💬 Answers: 1 📅 Created: 2025-06-14
json powershell performance PowerShell

I've been struggling with this for a few days now and could really use some help... I'm working on a project and hit a roadblock. I'm currently working with a large JSON file that contains thousands of entries, and I need to filter it based on specific criteria. The JSON structure is a list of objects, each containing properties like `Id`, `Name`, `Status`, and `CreatedDate`. My goal is to extract objects where `Status` is 'Active' and the `CreatedDate` is within the last 30 days. However, I'm running into performance optimization and the script seems to be quite slow. Here's the code I have so far: ```powershell # Load the JSON file data = Get-Content -Path 'C:\path\to\file.json' | ConvertFrom-Json # Get the current date and calculate the date 30 days ago $cutoffDate = (Get-Date).AddDays(-30) # Filter the data $filteredData = $data | Where-Object { $_.Status -eq 'Active' -and [datetime]$_.CreatedDate -ge $cutoffDate } | Select-Object Id, Name, CreatedDate # Output the filtered data enum $filteredData ``` When I run this code, it takes a long time to execute, especially on larger files. I noticed that converting from JSON and filtering could be the bottlenecks. I also tried using the `ForEach-Object` cmdlet as an alternative, but it didn’t seem to improve performance: ```powershell $data | ForEach-Object { if ($_.Status -eq 'Active' -and [datetime]$_.CreatedDate -ge $cutoffDate) { $_ } } | Select-Object Id, Name, CreatedDate ``` I received no behavior messages, but the execution time is still not optimal. Is there a more efficient way to handle large JSON files in PowerShell, or should I consider using a different approach or tool? Any suggestions would be greatly appreciated! What am I doing wrong? Is there a better approach?