0

I am writing a powershell script that takes in phone numbers stored in a csv file. The csv file has a "taken" field, indicating that the phone number is in use or not. Right now, I have a simple foreach loop that reads in the values of the taken field and changes the no to a yes:

Import-Csv '\\papertransport.com\files\UserDocuments\mneis\Code\phone_numbers2.csv' | ForEach-Object 
{
    if ($_.taken -eq 'no') 
    {
        $_.taken = 'yes'
        break
    }
    $_
} | Export-Csv \\papertransport.com\files\UserDocuments\mneis\Code\phone_numbers.csv -NoTypeInformation

My question is: how can I get it so the loop stops when when it hits the first no? The break obviously does not work. Also, is it possible to write to the same csv file instead of writing to a second one? When I write to the original it erases all the values inside of it.

4
  • Use foreach($item in Import-Csv ...){} rather than | ForEach-Object to make that break statement work Commented May 23, 2017 at 16:09
  • I have tried that and nothing changes in the csv file. Commented May 23, 2017 at 16:19
  • Your question is a little unclear. What exactly are you trying to accomplish? Do you want a csv file where only the very first occurrence of "no" has been changed to "yes", and every other row stays untouched? Commented May 23, 2017 at 16:20
  • @MathiasR.Jessen Sorry if it's a little unclear. Basically I want it to loop through the csv file and stop whenever it his a "no" in the taken column, breaking it out of the loop and leaving the rest of the "no's" untouched. It will then change that "no" to a "yes", so that the next time the csv file is iterated through it will do the same thing, this time passing over the previously changed "no". Does that make sense? Commented May 23, 2017 at 16:24

1 Answer 1

1

To get the loop, replace break with return, or change your code to the below.

Writing to the same file is possible; also in the code below. I would recommend making a backup of your existing file to ensure you don't lose any data. Added a try/catch statement that will do that, and stop the script if the copy cannot be created (you can also see a return in action there)

$myFile       = '\\papertransport.com\files\UserDocuments\mneis\Code\phone_numbers2.csv'
$backup       = '\\papertransport.com\files\UserDocuments\mneis\Code\$((Get-Date).ToString"yyyyMMdd")_phone_numbers2.csv'

try{
    Copy-item $myFile $backup -ErrorAction Stop
}catch{
    Write-Host "File could not be copied"
    return;
}


$phoneNumbers = Import-Csv $myFile

Foreach($number in $phoneNumbers)
{
    if ($number.taken -eq 'no') 
    {
        $number.taken = 'yes'
        break
    }

    # you will need to ensure all of the headers in your original file are here, otherwise they will not be in your output file
    [array]$myNewPhoneNumbers += New-Object psobject -Property{
             taken        = $number.taken
             FirstHeader  = $number.FirstHeader
             SecondHeader = $number.SecondHeader
    }

}


$myNewPhoneNumbers | Export-Csv '\\papertransport.com\files\UserDocuments\mneis\Code\$((Get-Date).ToString"yyyyMMdd")_phone_numbers.csv'

# I would recommend ensuring the code works how you want before uncommenting this.
#$myNewPhoneNumbers | Select-Object FirstHeader, SecondHeader, ThirdHeader |Export-Csv $myFile -Force
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.