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

  • Home
  • SEARCH
  • 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 860197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:46:52+00:00 2026-05-15T08:46:52+00:00

I am trying to do something very simple in PowerShell. Reading the contents of

  • 0

I am trying to do something very simple in PowerShell.

  1. Reading the contents of a file
  2. Manipulation some string
  3. Saving the modified test back to the file

    function Replace {
      $file = Get-Content C:\Path\File.cs
      $file | foreach {$_ -replace "document.getElementById", "$"} |out-file -filepath C:\Path\File.cs
    }
    

I have tried Set-Content as well.

I always get unauthorized exception. I can see the $file has the file content, error is coming while writing the file.

How can I fix this?

  • 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-15T08:46:53+00:00Added an answer on May 15, 2026 at 8:46 am

    This is likely caused by the Get-Content cmdlet that gets a lock for reading and Out-File that tries to get its lock for writing. Similar question is here: Powershell: how do you read & write I/O within one pipeline?

    So the solution would be:

    ${C:\Path\File.cs} = ${C:\Path\File.cs} | foreach {$_ -replace "document.getElementById", '$'}
    ${C:\Path\File.cs} = Get-Content C:\Path\File.cs | foreach {$_ -replace  "document.getElementById", '$'}
    
    $content = Get-Content C:\Path\File.cs | foreach {$_ -replace "document.getElementById", '$'}
    $content | Set-Content C:\Path\File.cs
    

    Basically you need to buffer the content of the file so that the file can be closed (Get-Content for reading) and after that the buffer should be flushed to the file (Set-Content, during that write lock will be required).

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

Sidebar

Related Questions

I am trying something very simple, but for some reason it does not work.
I'm trying something very simple. Upon page load the data in content div is
I'm trying to do something very simple, yet I can't seem to find the
I am trying to accomplish something very simple, and I'm hoping someone can point
I am new to Raphael and am trying to do something very simple, but
I am new to Raphael and am trying to do something very simple, but
Hey all, I am trying to accomplish something very simple yet getting an error
I am using flask , and trying to do something very simple using the
I'm trying to do something which is probably very simple, I have a directory
Currently I am trying something very simple. I am looking through an XML document

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.