0

I have a CSV file that looks like:

Initials,Size excl. Backup/Pst,Number of warnings
USER1,100,1
USER2,100,1
USER4,400,2

I would like to increase the value "Number of warnings" by 1 so I will end up with:

Initials,Size excl. Backup/Pst,Number of warnings
USER1,100,2
USER2,100,2
USER4,400,3

I have tried, but not succeeded.
I have also tried putting it in array, but can't get it working.

$Temp = Import-Csv $Database | foreach {$_.'Number of warnings' +1}

But this does 2 things:
only adds 1 to the end of the number so 1 becomes 11 and 2 becomes 21 (as it was a string)
The output is only the "Number of warnings" column - the rest of my information seems to be gone

4
  • 1
    I think you just forgot to assign the addition... {$_'Number of warnings' = $_'Number of warnings' +1} Commented Sep 4, 2013 at 13:07
  • Sorry Schwarzie2478, it fails. When I add the dot {$_.'Number of warnings' = $_.'Number of warnings' +1} it does not fail anymore, but $Temp is empty
    – Lisbjerg
    Commented Sep 4, 2013 at 13:11
  • You might be able to use foreach {$_.'Number of warnings'++} too Commented Sep 4, 2013 at 13:24
  • @AthomSfere I tried that, but it doesn't cast the string to an int and throws an error about the ++ operator only working on numbers.
    – alroc
    Commented Sep 4, 2013 at 14:16

1 Answer 1

1

This works :

$myreport = Import-Csv .\testing.csv

$myreport | %{$_.'Number of warnings' = 1 + $_.'Number of warnings'}
3
  • So putting the "1" in front, made PS change it from an string to an INT. clever...
    – Lisbjerg
    Commented Sep 4, 2013 at 13:32
  • @Lisbjerg you could also explicitly cast it to an int with [int]$_.'Number of warnings'+1
    – alroc
    Commented Sep 4, 2013 at 14:15
  • Just to make it clear, pay attention with the casting, make sure you don't cast the resulting string to integer. You'll have to add a lot of parenthesis to make sure, the casting happens before the addition... aah the joys of dynamic programming :-) Commented Sep 4, 2013 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.