Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8107211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:41:33+00:00 2026-06-06T00:41:33+00:00

I’m trying to run this script that basically reads/interprets text in from a textfile

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:41:37+00:00Added an answer on June 6, 2026 at 12:41 am

    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   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.