I am trying to replace a word (USERNAME with testing) inside my file.

I am trying to run following command from powershell but I am getting error ->

Get-Content is not recognized as an internal or external command

   get-content file.text | %{$_ -replace "USERNAME","testing"}

I replaced the command with following:

powershell.exe -Command file.text | %{$_ -replace "USERNAME","testing"}

Now I am getting error -> { $_ - is not recognized as an internal or external command

Also tried adding C:\Windows\System32 in environment variable but no luck.

Stuck & Need Help!

  • write foreach instead of % and see if that works. I guess you have a little batch/powershell mixup. % in batch is recognized as a variable – SimonS Aug 22 '17 at 8:28

I am pretty sure that you are running below command in cmd.exe (regular command-line) as is not recognized as an internal or external command is an error you would see in cmd.

get-content file.text | %{$_ -replace "USERNAME","testing"}

As for your second command, there is a qouting mixup there. The easiest way to fix it would be to replace double qoutes with single qoutes and have double qoutes around the -Command parameter arguments. Just like that:

powershell.exe -Command "Get-Content file.text | %{$_ -replace 'USERNAME','testing'}"
| improve this answer | |
  • Now after adding quotes I am getting this error At line:1 char:39 + ... Content file.text | {$_ -replace 'USERNAME', + ~~~~~~~~~~~~~~~~~~~~~~~~~ Expressions are only allowed as the first element of a pipeline. + CategoryInfo : ParserError: (:) [], ParentContains + FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline – Mishi Aug 22 '17 at 7:59
  • I am running it in powershell – Mishi Aug 22 '17 at 8:00
  • Can you paste the exact command you are running? – WeatherForecastingRat Aug 22 '17 at 8:08
  • SET CONFIG_FILE_NAME=file.conf – Mishi Aug 22 '17 at 9:32
  • powershell.exe -Command "Get-Content C:\Users\Admin\Downloads\ne\w-alertshipperWSO2.conf | foreach-object {$_ -replace '_USERNAME','testing' } | Set-Content C:\Users\Admin\workspace\w-alertshipperWSO2.conf -Force" – Mishi Aug 22 '17 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.