Embercide
Embercide

Reputation: 65

Powershell replace string from array inside foreach loop

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?

Upvotes: 0

Views: 1248

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174435

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,"")
}

Upvotes: 1

Related Questions