CSV into DGV

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
JohnTitor
Posts: 163
Last visit: Tue Jul 27, 2021 5:57 am

CSV into DGV

Post by JohnTitor »

Hi,
I have CSV it contains USERNAME, Manufacturer, Model, hundreds of rows of different Names, Manufacturers and Models. We're looking to quickly few the counts of each model types.

So something similar would be

JohnSmith , HP , 840G8
JaneSmith , Apple , 15 inch iPad Pro
DarringSmith, Dell, 7510

How can I view in a DGV, just the Model and the Counts, so example:
Apple 15 inch iPad Pro 40
HP 840 G8 1500
Dell 7470 1500

jvierra
Posts: 14885
Last visit: Sun Aug 01, 2021 3:12 am
Answers: 12
Has voted: 3 times
Been upvoted: 10 times

Re: CSV into DGV

Post by jvierra »

Use the "Group-Object" CmdLet to get the counts.

User avatar
JohnTitor
Posts: 163
Last visit: Tue Jul 27, 2021 5:57 am

Re: CSV into DGV

Post by JohnTitor »

I did find group-object

but used group-object Model
am getting this
Assets.png
Assets.png (15.04 KiB) Viewed 2720 times
anyway to shorten the code?
  1. import-csv '\\share\assets.csv' | Select-Object Manufacturer, Model, Count | Group-Object Model | ForEach-Object {
  2. [PSCustomObject]@{
  3.  
  4. 'Count' = $_.Count;
  5. 'Model' = $_.Name;
  6. 'Manufacturer' = $_.group.manufacturer | Select-Object -Unique;
  7. }
  8. } | Sort-Object Count -Descending

jvierra
Posts: 14885
Last visit: Sun Aug 01, 2021 3:12 am
Answers: 12
Has voted: 3 times
Been upvoted: 10 times

Re: CSV into DGV

Post by jvierra »

Start by writing the code correctly.
  1. import-csv '\\share\assets.csv' |
  2.     Group-Object Model,Manufacturer |
  3.     Sort-Object Count -Descending |
  4.     Select-Object Name, Count

User avatar
JohnTitor
Posts: 163
Last visit: Tue Jul 27, 2021 5:57 am

Re: CSV into DGV

Post by JohnTitor »

Hi, thanks for the tip
but its not formatted correctly still

note the manufactuer is in the same column as name
Assets.png
Assets.png (9.61 KiB) Viewed 2690 times
and not like this :)
assets2.png
assets2.png (7.23 KiB) Viewed 2690 times

jvierra
Posts: 14885
Last visit: Sun Aug 01, 2021 3:12 am
Answers: 12
Has voted: 3 times
Been upvoted: 10 times

Re: CSV into DGV

Post by jvierra »

So. Split the Name into two fields using computed properties in the last select-object statement.

User avatar
JohnTitor
Posts: 163
Last visit: Tue Jul 27, 2021 5:57 am

Re: CSV into DGV

Post by JohnTitor »

Got it! Thanks so much

Locked