Comments (8)
Compare with
pwsh -c "Get-Process" >o.csv
which takes less than a second.
Now compare that with the information written with
Get-Process | ConvertTo-JSON | Set-Content -path "o.json"
And now it takes for ages because for each process it tries to get detailed information including what DLLs are loaded into each process, threads in the process, memory usage etc..
Compare that with
get-process | foreach-object { Write-Output $_.Id , $_.MainModule.ModuleName }
Which is less than a second again.
So in summary, be careful what you ask for. Get-Process is quick until you start inquiring for detailed information for each process, which Export-CSV and ConvertTo-JSON will do because they are generic.
So try
get-process | Select-Object -Property Id,MainModule | Export-Csv -LiteralPath o.csv
Which takes less than a second again
However
get-process | Select-Object -Property Id,CommandLine | Export-Csv -LiteralPath o.csv
Takes for ages, so ask for what you need.
from powershell.
So in summary, be careful what you ask for. Get-Process is quick until you start inquiring for detailed information for each process, which Export-CSV and ConvertTo-JSON will do because they are generic.
Thanks, that's helpful. It seems that there's a subset of approximately 10 properties, out of a total of 69, that especially slow down the read process (the 'Module' and 'Parent' properties are the slowest among them). When these 10 properties are excluded, the operation completes in just a second or two. Including all 10 them causes the total execution time to skyrocket to about 200 seconds!
PS C:\tmp> $allProps = (gps | gm -MemberType properties).Name;
PS C:\tmp> $slowProps = ('Modules', 'MainModule', 'Parent', 'CommandLine', 'Company', 'Path', 'Description', 'Product', 'ProductVersion', 'FileVersion');
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 0)}) | Export-Clixml o.xml}).TotalSeconds
1.7832752
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 1)}) | Export-Clixml o.xml}).TotalSeconds
3.719695
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 2)}) | Export-Clixml o.xml}).TotalSeconds
5.3285927
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 3)}) | Export-Clixml o.xml}).TotalSeconds
7.072522
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 4)}) | Export-Clixml o.xml}).TotalSeconds
8.9156198
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 5)}) | Export-Clixml o.xml}).TotalSeconds
10.5895199
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 6)}) | Export-Clixml o.xml}).TotalSeconds
12.3948109
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 7)}) | Export-Clixml o.xml}).TotalSeconds
35.2305779
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 8)}) | Export-Clixml o.xml}).TotalSeconds
91.9951524
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 9)}) | Export-Clixml o.xml}).TotalSeconds
92.1810648
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 10)}) | Export-Clixml o.xml}).TotalSeconds
206.8004993
from powershell.
In my opinion it works pretty fast:
$ Get-Process | Export-Csv -path "o.csv"
$ (Get-History -Count 1).Duration.TotalMilliseconds
411.0004
$ (Import-Csv ./o.csv | Get-Member -MemberType NoteProperty).Count
69
$ (Get-Content ./o.csv).Count
203
$ (Get-Item ./o.csv).Size
117463
7.5.0-preview.1 on Linux.
from powershell.
this is not the same as the original posters environment
I thought about this aspect. But I canβt test on Windows (replaced it with Linux).
I posted it just for comparison and the overall picture.
from powershell.
@rhubarb-geek-nz provided the right solution which is to only serialize what you actually need. Getting the .NET object is fast because some properties are just references that aren't retrieved until actually used like during serialization.
from powershell.
In my opinion it works pretty fast:
7.5.0-preview.1 on Linux.
Yes, this is not the same as the original posters environment on Win10 machine (PSVersion 7.4.2; OS version 10.0.19045),
Process management on Windows is completely different to that on Linux, both conceptually and the API used to retrieve the information.
from powershell.
This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.
from powershell.
π£ Hey @yg-i, how did we do? We would love to hear your feedback with the link below! π£οΈ
π https://aka.ms/PSRepoFeedback
from powershell.
Related Issues (20)
- exit status `0` on syntax errors, when reading scrip from stdin HOT 4
- `$ErrorActionPreference = 'Stop'` ignored, when command read from stdin. HOT 15
- `-CommandWithArgs` CLI parameter doesn't work with `-` (code provided via stdin)
- fwlink helpURI of Get-Uptime,Get-FileHash and others doesn't work HOT 5
- Unable to Install PowerShell 7.4 with Ubuntu 24.04 HOT 6
- Incorrect Auto-Completion When Using Tab HOT 4
- Mount-DiskImage does not assign driver letter every third time HOT 4
- Does PowerShell support the real "Stream" response? HOT 6
- v7.4.2 not in Ubuntu 22.04 Linux package repository for Microsoft products HOT 3
- `Finally` block isn't executed upon Ctrl+C HOT 36
- Make `ConvertFrom-StringData` more error tolerant HOT 9
- Add-Type ReferencedAssemblies only works with full file paths now? HOT 1
- .Net 8 in PowerShell 7 System.Private.CoreLib fails to register. HOT 41
- `ConvertFrom-StringData` mistakenly emits a statement-terminating error when one of its input strings isn't well-formed HOT 2
- Redirection operator `>` should not be hardcoded to files HOT 6
- Update from System.Data.SqlClient to Microsoft.Data.SqlClient HOT 3
- .NET App using Powershell.SDK fails to run, when building as SingeFile HOT 3
- Add a command which gets the System Lockdown mode information HOT 5
- Overriding .NETs ToString() method can cause unexpected behaviour when using Transcripts HOT 1
- `Compare-Object` doesn't compare custom value types (`structs`) (that don't implement `IComparable`) correctly HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from powershell.