powershell foreach-object with if statements

17,812

Solution 1

You need to remove leading and (or) trailing spaces. Try this:

$ht=@{}

Get-Content $filename | foreach{

    $key,$value = $_.Split('=') | foreach {$_.Trim()}

    if($key -like "line2")
    {
       $key = "line2 has been changed"
    }

    if($value -like "value2")
    {
        $value = "value2 has been changed"
    }

    $ht.Add($key,$value)
}

$ht.GetEnumerator() | Sort-Object name


Name                           Value                                                                                                                                     
----                           -----                                                                                                                                     
line1                          apple                                                                                                                                     
line10                         igloo                                                                                                                                     
line2 has been changed         firetruck                                                                                                                                 
line3                          cricket                                                                                                                                   
line4                          gorilla                                                                                                                                   
line5                          elephant                                                                                                                                  
line6                          banana                                                                                                                                    
line7                          jumper                                                                                                                                    
line8                          hat                                                                                                                                       
line9                          deer   

Solution 2

I think the trailing space throwing you off.

Change this:

$key = $_.Substring(0,$equalindex)

to this:

$key = $_.Substring(0,$equalindex).Trim(' ')

or maybe the index is off as this seems to work too:

$key = $_.Substring(0,$equalindex - 1)
Share:
17,812
shreddish
Author by

shreddish

Updated on June 04, 2022

Comments

  • shreddish
    shreddish almost 2 years

    I'm trying to run this script that basically reads/interprets text in from a textfile and stores the "key" and "value" into a hashtable. Now I want to change a specific key and value as I am reading the text from the text file.

    The following is the text in my file:

    line1 = apple  
    line2 = firetruck  
    line3 = cricket  
    line4 = gorilla  
    line5 = elephant  
    line6 = banana  
    line7 = jumper  
    line8 = hat  
    line9 = deer  
    line10 = igloo  
    

    Here is my code:

    param
    (
        [Parameter(Mandatory=$true,  HelpMessage="Please specify the text filename.")]$fileName
    )
    
    
    $hashTable = @{}
    $textFile = Get-Content $filename | ForEach-Object `
    {
        $equalindex = $_.IndexOf("=")
        $key = $_.Substring(0,$equalindex)
    
        if($key -like "line2")
        {
            $key = "line2 has been changed"
        }
    
        $remainingLen = (($_.Length - 1) -$equalindex)
        $value = $_.Substring($equalindex +1, $remainingLen)
    
        if($value -like "value2")
        {
            $value = "value2 has been changed"
        }
    
        $hashtable.Add($key,$value)
    } 
    
    $hashtable.Set_Item("line5","line5 value has been changed")
    $hashtable.GetEnumerator() | Sort-Object name
    

    When I run my code it outputs the hashtable but does not change my "line2" key or my "value2" value. Is this the correct/most logical way to do what I'm trying to accomplish?

  • shreddish
    shreddish almost 12 years
    @ray023 I'm sorry guys I included those spaces when I was posting the text so it would add a line break but then forgot to take it out when the site told me it wasn't formatted right. (I needed to add the "code" indent to my text file text). So the text in my textfile doesn't have any trailing spaces. I'm sorry for the confusion
  • shreddish
    shreddish almost 12 years
    ahh sorry Shay and @ray023 I saw what you were saying I just implemented the code and it worked thank you. I will have to read up on the Split command a little more. i also just realized I was searching for text "value2" instead of "firetruck" which was the actual value for line 2 which is why that value wasnt't changing. Thanks for your quick responses I appreciate the help