1

I am trying to run a powershell command inside cmd script for replacing a text file content

set file="C:\myfile.txt"

powershell -command "(Get-Content %file%) | ForEach-Object { $_ -replace "Latest", "FOO" } | Set-Content %file%"

I am getting the following error:

You must provide a value expression on the right-hand side of the '-replace' operator.

What am I doing wrong?

Thanks

EDIT (not sure if relevant to the answer...)

The file content is

\\10.10.10.10\Shared\Latest

1 Answer 1

2

You are not nesting strings correctly

powershell -command "(Get-Content %file%) | ForEach-Object { $_ -replace "Latest", "FOO" } | Set-Content %file%"
                    ^----------------------------------------------------^
               string start                                        string end

Use single quotes:

powershell -command "(Get-Content %file%) | ForEach-Object { $_ -replace 'Latest', 'FOO' } | Set-Content %file%"
Sign up to request clarification or add additional context in comments.

1 Comment

@AnsgarWiechers it seems to work as %file% - I guess the command prompt does the substitution for that before calling powershell.exe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.