0

In PowerShell I'm importing a CSV SAMTemp2 which will have a field called SO. Sometimes SO will be populated with "NW" and in these cases I just want to pull the field called ProdProj from the same line and replace the data in SO with the data in ProdProj then export it the data in that condition.

$RepNW = Import-Csv $SAMTemp2
foreach($d in $data){
If($d.SO -eq "NW"){($d.SO).Replace($d.ProdProj)}}
$RepNW | Export-Csv $SAMTemp -NoTypeInformation

I don't get an error, but this doesn't seem to do anything, either. Can anyone assist me, please?

Update

Per Matt below, I tried:

$RepNW = Import-Csv $SAMTemp2
foreach($d in $RepNW){
If($d.SO -eq "NW"){$d.SO = ($d.SO).Replace($d.ProdProj)}}
$RepNW | Export-Csv $SAMTemp -NoTypeInformation

But I'm not seeing any change. Any assistance is appreciated.

5
  • You are not writing the changes back. $d.SO = ($d.SO).Replace($d.ProdProj) should do it. Other improvement might help as well. Commented Mar 22, 2017 at 20:02
  • 1
    where does $data come from? You read into var $RepNW Commented Mar 22, 2017 at 20:21
  • @LotPings >.<; thanks. I missed that. Commented Mar 22, 2017 at 20:58
  • @Matt where do I put that? I tried If($d.SO -eq "NW"){$d.SO = ($d.SO).Replace($d.ProdProj)}} but the fields still report as "NW". Commented Mar 22, 2017 at 21:00
  • Your replace is flawed maybe. Are you just chenging the value completely ? Why are you using replace Commented Mar 22, 2017 at 21:10

1 Answer 1

3

As LotPings pointed out in this line foreach($d in $data){, you haven't defined $data and it seems that you mean it to be foreach($d in $RepNW){

Secondly, rather than using Replace() you can just set one property to be equal to the other.

Last, this probably easiest to do all in the pipeline with ForEach-Object

Import-Csv $SAMTemp2 | ForEach-Object {
    If($_.SO -eq "NW"){
        $_.SO = $_.ProdProj
    }
    $_
} | Export-Csv $SAMTemp -NoTypeInformation
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks all! This was the answer. And @Matt you're right about my flawed replace. I have a lot of flaws, and not just in scripting.

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.