How to replace path in text file with Powershell - powershell

I have a problem with replace text in Powershell. For e.g.
When I use:
(Get-Content C:\TEMP\App.config) -replace "one","two" | Set-Content C:\TEMP\App.config
it works. But when I use:
(Get-Content C:\TEMP\App.config) -replace "C:\Program Files (x86)\Adobe\Acrobat Reader 10\Reader\AcroRd32.exe","C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" | Set-Content C:\TEMP\App.config
is doesn't work. I search a lot of info in Google or documentation but still have a problem.
Can anybody help? :-)

You should be aware that the -replace command uses regex. It should work, if you escape the replace string:
$searchString = [Regex]::Escape('C:\Program Files (x86)\Adobe\Acrobat Reader 10\Reader\AcroRd32.exe')
$replaceString = 'C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe'
(Get-Content C:\TEMP\App.config) -replace $searchString, $replaceString | Set-Content C:\TEMP\App.config

The -replace operator uses regex on the search string and replaces that with another string.
That replacement string is a normal, unescaped string.
Since your path has characters in it that have special meaning in regex, like the backslash, you need to escape the search string by prepending a backslash in front of every such special character.
Regex has its own static method for that: [regex]::Escape()
Also worth mentioning is that if you read the file including the -Raw switch, so it wil become a single multline string, replacing values is much faster.
$search = [Regex]::Escape('C:\Program Files (x86)\Adobe\Acrobat Reader 10\Reader\AcroRd32.exe')
$replace = 'C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe'
(Get-Content -Path 'C:\TEMP\App.config' -Raw) -replace $search, $replace | Set-Content -Path 'C:\TEMP\App.config'

Related

Powershell -Replace command and escaping characters

I need to replace some strings multiple text files. I am running into an issue with the following string though. I tried escaping with a backtick ` before any special characters, but no luck, or maybe the backtick isn't in the right place.
I want to replace
Signal\s+:\s+',''
with
Signal\s+:\s+','';if (!`$signal) {`$signal='n/a'}
This is the string in the command that i am having a problem finding --> Signal\s+:\s+',''
Here is the powershell command i am using..
Get-ChildItem "f:\temp\*.ps1" | ForEach-Object -Process {(Get-Content $_) -Replace "Signal\s+:\s+',''" , "Signal\s+:\s+','';if (!`$signal) {`$signal='n/a'}" | Set-Content $_}
thank you
i just needed to escape the \ in the replace text.. thats all
-Replace "Signal\\s\+:\\s\+',''"
thanks

How to replace a newline at the start of a txt file with powershell

I have tried a lot, but I cannot seem to replace the new line at the start of a txt file.
So my txt file looks like this:
I just want to remove the first newline character, but everything I try does not work:
Replace ``n`r, replace \n\r or any combination of these.
Try
(Get-Content -Path 'YourFile.txt' -Raw).TrimStart() | Set-Content -Path 'YourFile.txt' -Force
Or
(Get-Content -Path 'YourFile.txt' -Raw) -replace '^\s+' | Set-Content -Path 'YourFile.txt' -Force
Explanation:
The above removes all whitespace (tabs, spaces, newlines) from the top of the text, as it is impossible to see from the image if other whitespace characters are in that line or not.
If you are sure there is just the one newline, in your case \r\n won't work, because the file uses Unix newlines (\n only).
Better is to replace using ^\r?\n. The ^ anchors at the beginning of the text. The ? reads zero or one on the CR character \r
This would replace all blank lines. The parentheses make sure the first command finishes first if you're writing to the same file.
(get-content file.txt) | where { $_ } | set-content file.txt
Or this way, the filename goes first.
set-content file.txt (get-content file.txt).where{$_}
A different approach
( Get-Content -Tail ( ( Get-Content testfile1 ).count-1 ) testfile1 ) | Set-Content testfile1
Count the number of lines in the file and then take one off the total. Use that to tail the file and write the output back to the file.
If you are certain that there will be an empty line (or you want to ignore it) you can use Skip.
Get-Content -Path 'YourFile.txt' | select -Skip 1 | Set-Content -Path 'YourFile.txt' -Force

How do I save a command output to a variable, and how do I use that variable as command parameter in Powershell?

I'd like to replace a word inside a .cfg file with the name of the current user.
I know $env:username outputs the current user.
This is what I got:
(Get-Content -path path\example.cfg -Raw) -replace 'TEST','current_user' | Set-Content path\example.cfg
I'm trying to replace the word "TEST" with the current user.
Just replace your literal string 'current_user' with $env:username and you're good to go :)
(Get-Content -path path\example.cfg -Raw) -replace 'TEST',$env:username | Set-Content path\example.cfg
Note that -replace is a regex operator, so we can even qualify the word we're looking for:
(Get-Content -path path\example.cfg -Raw) -replace '\bTEST\b',$env:username | Set-Content path\example.cfg
\b means "word boundary" in regex, and will help you avoid partially replacing TEST in words like "HOTTEST" or "TESTICULAR"

Find Carriage Returns in Files Using Powershell

I want to see all the instances of files containing the Windows-style crlf instead of Unix-style lf in a set of files. Here's what I have so far:
sls -Path src/*.cs -Pattern "`r`n" | group Path | select name
This works if I search for any normal text, but it's not finding the carriage returns, even though (according to everything I can find online) that's the proper Powershell escape sequence for carriage returns and newlines. For the record \r\n doesn't work either.
sls (an alias for Select-String) works line by line, so it's already processing (consuming) the line breaks during the file reading process before it gets to the regex matching.
Use something that reads the entire file, and then look for it:
Get-ChildItem -Path src/*.cs | ForEach-Object {
$contents = [System.IO.File]::ReadAllText($_.FullName)
if ($contents -cmatch '\r\n') {
$_
}
} | Group-Object Directory | Select-Object Name
\r\n is used here instead of the backticks because you're escaping them for the regex engine, not for powershell.

Replace . with new lines in text file using powershell

I have a text file containing names of people seperated by . how to replace the . with new lines so that each name is in new line with powershell
If you have the latest version of PSCX (3.2.0) http://pscx.codeplex.com, we just added a new command to simplify this type of task a bit:
Edit-File -Path c:\path\to\file.txt -Pattern '\.' -Replacement "`r`n"
Or using positional params:
Edit-File c:\path\to\file.txt '\.' "`r`n"
This command also handles taking care that the file's original encoding is preserved. Using Out-File will output using Unicode unless you override with the -Encoding parameter which of course requires that you know the file's encoding in the first place. :-)
You can do a simple replace...
(gc c:\path\to\file.txt) -replace "\.","`n" | out-file c:\path\to\newfile.txt