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 6579869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:57:08+00:00 2026-05-25T15:57:08+00:00

Okay, so I’ve set up a hash table with names being what to replace

  • 0

Okay, so I’ve set up a hash table with names being what to replace and keys being what to replace with, like this:

$r = @{
    "dog" = "canine";
    "cat" = "feline";
    "eric" = "eric cartman"
}

What should I do next? I’ve tried this:

(Get-Content C:\scripts\test.txt) | Foreach-Object {
    foreach ( $e in $r.GetEnumerator() ) {
        $_ -replace $e.Name, $e.Value
    }
} | Set-Content C:\scripts\test.txt.out

But it doesn’t work at all, it just writes each line three times, without replacing anything.

EDIT: Contains of test.txt:

dog
cat
eric

test.txt.out:

dog
dog
dog
cat
cat
cat
eric
eric
eric
  • 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-05-25T15:57:09+00:00Added an answer on May 25, 2026 at 3:57 pm

    Here’s one way to do it:

    $file = Get-Content C:\scripts\test.txt
    foreach ($e in $r) {
      $file = $file -replace $e.Name, $e.Value
    }
    Set-Content -Path C:\scripts\test.txt.out -Value $file
    

    The reason you were seeing each line three times is because of the nested foreach loop. A replace operation was running once per hashtable entry for every line in the file. That doesn’t change the source file, but by default it does output the result of the replace (even if nothing is changed).

    You can get the desired functionality by reading the file into a variable first, and then using your looping replace to update that variable. You also don’t need a separate foreach loop for the file contents; the replace can run against the full text in one pass per hashtable entry.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay. I know this looks like the typical Why didn't he just Google it
Okay here is my url: http://example.com/home/process_login I would like to replace the underscore with
Okay, so I've known about this for like 600 years now, but I've only
Okay so im working on this php image upload system but for some reason
Okay, so this probably sounds terribly nefarious, but I need such capabilities for my
Okay, so I'm making a table right now for Box Items. Now, a Box
Okay I do not even know where to begin when it comes to this
Okay, so I have an issue with an AJAX request. I currently have this
Okay, none of the previous questions I have seen with this error seem to
Okay, so this is a long long shot but here it goes... When IE

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.