0

I have a CSV file containing products, this CSV file has a column for brands and a column for categories. I'm trying to run a foreach loop to remove the brand names from the categories column.

$products = import-csv X:\products.csv
$brands = $products.Brand | sort | select -unique

foreach ($item in $products) {
  $item.categories = $item.categories.Replace("Coming Soon","")
  $item.categories = $item.categories.Replace($brands,"")
}

The "Coming Soon" gets removed, but all the brand names that are containd in $brands still remain.

Where am I going wrong?

1 Answer 1

1

Right now you're attempting to remove all the brand names from each categories property.

Reference $item.Brand inside the foreach loop to get the corresponding one:

foreach($item in $products){
  $item.categories = $item.categories.Replace("Coming Soon","")
  $item.categories = $item.categories.Replace($item.Brand,"")
}
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, such a simple thing that I've overcomplicated! Just so I'm clear on what I was doing wrong - it wasn't working because it was looking for 200+ brand names in a single property?

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.