# Array modifications based on conditions

**URL:** <https://forums.powershell.org/t/array-modifications-based-on-conditions/25012>\
**Category:** PowerShell Help\
**Created:** [November 13, 2024, 2:30am UTC](https://forums.powershell.org/t/array-modifications-based-on-conditions/25012 "2024-11-13T02:30:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lacourchl](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@lacourchl](https://forums.powershell.org/u/lacourchl)\
**Post date:** [November 13, 2024, 2:30am UTC](https://forums.powershell.org/t/array-modifications-based-on-conditions/25012/1 "2024-11-13T02:30:17Z")

</div>

hello,  
I’m looking for some guidance on how to modify an array initially loaded from Excel. The modifications are based on conditions, ie, so for a real simple example, if $item.Unit contains a dash, then change the value of B, etc.

Here’s a simplified version of the code:  
$dataArray = Import-Excel -Path $excelInPath -Startrow 2 -StartColumn 1 -EndColumn 20  
foreach ($item in $dataArray) {  
$msg = new-object -Typename psobject -property @{  
A = $item.Unit;  
B = $item.Country;  
C = $item.Dept;  
D = $item.Level;  
}  
if($msg.Unit.contains(“-”)) {  
$msg.Country = 200;  
}  
I’m getting a runtime error if I leave the if() statement, but do need a way to modify the array. Any input is greatly appreciated.  
Many thanks.  
Lacour

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [November 13, 2024, 2:51am UTC](https://forums.powershell.org/t/array-modifications-based-on-conditions/25012/2 "2024-11-13T02:51:50Z")

</div>

Hello, welcome to the forum. When posting code or errors, please use the preformatted option (looks like this `</>` and may be hidden under the settings gear.)

I see three issues with your code.

1. It appears you’ve copied this code from a website that didn’t use a good formatting for code. The double quotes are “fancy”. Just type those out manually to replace them.  
`“-”` bad quotes  
`"-"` good quotes

2. You are missing a curly brace. This is much easier to spot when you properly indent the code. I didn’t notice this issue until I went to fix it and it became obvious.

3. You are referencing the wrong property name. In your `$msg` object, you have 4 properties. A, B, C, and D.

```auto
$dataArray = Import-Excel -Path $excelInPath -Startrow 2 -StartColumn 1 -EndColumn 20

foreach ($item in $dataArray) {
    $msg = new-object -Typename psobject -property @{
        A = $item.Unit
        B = $item.Country
        C = $item.Dept
        D = $item.Level
    }
    if($msg.A.contains("-")) {
        $msg.B = 200
    }
}

```

As an aside, the semicolon’s are not required here.

---

<div class="post-metadata">

**Author:** ![lacourchl](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@lacourchl](https://forums.powershell.org/u/lacourchl)\
**Post date:** [November 20, 2024, 2:24am UTC](https://forums.powershell.org/t/array-modifications-based-on-conditions/25012/3 "2024-11-20T02:24:18Z")

</div>

> [@krzydoug](#):
>
> - It appears you’ve copied this code from a website that didn’t use a good formatting for code. The double quotes are “fancy”. Just type those out manually to replace them.  
> `“-”` bad quotes  
> `"-"` good quotes
> - You are missing a curly brace. This is much easier to spot when you properly indent the code. I didn’t notice this issue until I went to fix it and it became obvious.
> - You are referencing the wrong property name. In your `$msg` object, you have 4 properties. A, B, C, and D.

Thank you Doug, I have updated my script based on your suggestions, and script is running great - thank you so much - Chris

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [December 20, 2024, 2:25am UTC](https://forums.powershell.org/t/array-modifications-based-on-conditions/25012/4 "2024-12-20T02:25:09Z")

</div>

Closed on December 20, 2024, 2:25am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
