0

I'm a powershell beginner and I hope you can help me. I wrote a PS script for our accounting department that shows the license costs for our microsoft 365 users with the cost center number of the user. I would like to expand the script by automatically creating the totals for each cost center via csv import in powershell. here is what the csv list looks like after the script runcsv after the script run Every Cost Center can have multiple, different types of licenses. So for example the cost center "1051080" could have ten times the license "business premium" and five times the license "F3" as you can see in the image.

At the end i want to have a summary for each cost center that looks somehow like that final CSV This summary could be append at the end of the origin csv file or exported to a new one. that does not matter. I hope you understand what i mean. English is not my mother tongue.

Maybe there is a completely different way to achieve this and powershell is not even necessary. I am thankful for every help. Best regards and greeting! Felix

  • 1
    Hi Felix, could you share the script that you use? Maybe that will make it easier to recommend how to alter your script. – CdkK Apr 20 '23 at 09:10
  • 1
    In addition to "Post what you have tried so far", it's always a good idea to avoid posting source data (your CSV) as images. People are less likely to post solutions if they can't copy/paste sample data. Instead, use the text tags in markdown. See [how to ask a good question](https://stackoverflow.com/help/how-to-ask) – MyICQ Apr 20 '23 at 09:36
  • Please provide enough code so others can better understand or reproduce the problem. – Community Apr 20 '23 at 23:56
  • Sorry for my bad initial question! I will edit my post and share the code/script as soon as i am back in office. – theHappyOne Apr 21 '23 at 05:15

1 Answers1

0

Try :

$filename = 'c:\temp\test.csv'
$csv = import-csv -Path $filename

$columnsToAdd = @('Amount F3 License','Amount Business Prem')

$totalRow = New-Object -TypeName PSObject
foreach($columnToAdd in  $columnsToAdd)
{
   $total = $csv | Measure-Object -Sum -Property $columnToAdd*
   $totalRow | Add-Member -NotePropertyName $columnToAdd -NotePropertyValue $total.Sum
}
$csv
$totalRow | Format-List
jdweng
  • 33,250
  • 2
  • 15
  • 20