Replace `set` statements to `setx` in a batch script using powershell - powershell

Using powershell, I want to take a .bat file and replace all lines in which set is called to set an environment variable and change it to a corresponding setx call. Unfortunately it's not as simple as just doing a search and replace on the file, replacing set for setx, because the syntax is diffferent: set ENVNAME=abc vs setx ENVNAME abc.
Is there a simple way to do this in powershell? To just do the set for setx replacement, I have:
(Get-Content $orig_filename ) | ForEach-Object {$_ -replace "set", "setx"} | Set-Content $new_filename
Any pointers for a powershell novice would be appreciated.

Did some pretty limited testing and I'm not sure what your bat file looks like, but this may work for you.
(Get-Content $orig_filename ) | ForEach-Object {
$_ -replace 'set (.*)\=(.*)','setx $1 $2'
} | Set-Content $new_filename

You could also just chain another -replace to do the work for you. Also made the set > setx replacement a little more less error prone by ensuring you are replacing the word set at the beginning of the string. Since -replace functions as an array operator you do not need the foreach loop.
(Get-Content $orig_filename ) -replace "^set\b", "setx" -replace "="," " | Set-Content $new_filename

Related

Powershell: Editing all Line Feeds and Carriage Returns in a CSV file

Goal is to remove all single "Line Feeds"(LFs) and keep the "Line Feeds" which follow a Carriage Return(CR) in a csv file.
I got a report which includes several LFs in one row, but I only want to keep the "CR+LF" so every row stands for one reported object.
I need a solution in PowerShell, unfortunately I am very new to PowerShell scripting. I tried to alter some scripts on this side for this job, but its not working.
First, I would try to remove all LFs in the file, and then I would replace all remaining CRs with [CR][LF]. But I did not achieve the first step.
$original_file ='C:\Test\Server.csv'
$new_file = 'C:\Test\Server_changed.csv'
(Get-Content $original_file -Raw).Replace('´n',' ') | Set-Content $new_file -Force
(Get-Content $new_file -Raw).Replace('`r','`r`n') | Set-Content $new_file -Force
Source CSV:
"Servername";"CPU","Memory";"Annotation";"OperatingSystem"[CR][LF]
"Server1";"4";"8";"very importand Server!![LF]
If reboot is needed:[LF]
1. Contact Me[LF]
2. Stop all running Services before shutting down the OS[LF]
";"Windows Server 2019";[CR][LF]
How it should look:
"Servername";"CPU","Memory";"Annotation";"OperatingSystem"[CR][LF]
"Server1";"4";"8";"very importand Server!! If reboot is needed: 1. Contact Me 2. Stop all running Services before shutting down the OS ";"Windows Server 2019";[CR][LF]
You can use the -replace operator multiple times to achieve the result.
$original_file ='C:\Test\Server.csv'
$new_file = 'C:\Test\Server_changed.csv'
(Get-Content $original_file -Raw) -replace "(?<!\r)(\n)" -replace "\r(?!\n)","`r`n" |
Set-Content $new_file -NoNewLine -Force
Explanation:
-replace is the regex replace operator as opposed to the string class .Replace(). -replace is used so that we can access the regex mechanisms negative lookahead ((?!)) and negative lookbehind ((?<!)). In each -replace operation, the first set of quotes represent the regex pattern to capture the data you want to replace. The second set of quotes represent the replacement string. If you specify no second set of quotes, then the captured data will just be removed.
-Raw switch is used in Get-Content to prevent PowerShell from reading the file in as an array, which will add newline characters to the data in memory.
-NoNewLine switch on Set-Content is used to not add an additional, trailing newline character at the end of the output file.

How can i run multiple replace commands from one line of batch file

I have minimum to none knowledge of powershell :(
Hi I have two possible options to replace text from an .ini file, one is a menu-style batch, where choosing an option will execute a command.
My problem is: if I use the batch code I can only change a known resolution, because I don't know how to add multiple replace actions so they work if one fails.
The Powershell code does executes MULTIPLE replace commands, but I don't know how to edit it to use it as a batch command (powershell -command etc.)
Thank you in advance :)
Batch script:
#echo off
set ffile='resolutions.ini'
set HDReady='/resolution:1280,720'
set FullHD='/resolution:1920,1080'
set QuadHD='/resolution:2560,1440'
set UltraHD='/resolution:3840,2160'
powershell -Command "(gc %ffile%) -replace %hdready%, %fullhd% | Out-File %ffile% -encoding utf8"
Powershell script:
$original_file = 'path\resolutions.ini'
$destination_file = 'path\resolutions.ini'
(Get-Content $original_file) | Foreach-Object {
$_ -replace '/resolution:1280,720', '/resolution:1920,1080' `
-replace '/resolution:2560,1440', '/resolution:1920,1080' `
-replace '/resolution:3840,2160', '/resolution:1920,1080'
} | Set-Content $destination_file
Is one (1) line what you really want?
(Get-Content 'path\resolutions.ini') | Foreach-Object {$_ -replace '/resolution:1280,720', '/resolution:1920,1080' -replace '/resolution:2560,1440', '/resolution:1920,1080' -replace '/resolution:3840,2160', '/resolution:1920,1080'} | Set-Content 'path\resolutions.ini'
or
$original_file = 'path\resolutions.ini'; $destination_file = 'path\resolutions.ini'; (Get-Content $original_file) | Foreach-Object {$_ -replace '/resolution:1280,720', '/resolution:1920,1080' -replace '/resolution:2560,1440', '/resolution:1920,1080' -replace '/resolution:3840,2160', '/resolution:1920,1080'} | Set-Content $destination_file
You can also place your script in a file and run it as such:
powershell.exe -file My\FilePath.ps1
Here is the help section from the powershell executable:
PowerShell -Help
-File
Runs the specified script in the local scope ("dot-sourced"), so that the
functions and variables that the script creates are available in the
current session. Enter the script file path and any parameters.
File must be the last parameter in the command, because all characters
typed after the File parameter name are interpreted
as the script file path followed by the script parameters.

Simplify PowerShell Script with Replace Statements

I am trying to run this script on a 50GB file in Windows 2012 R2 and I would like to hopefully get the three replace statements into one pass rather than three. Also, it is important that the replaces occur in that order. Any suggestions to simplify this and make it run efficiently would be greatly appreciated!
$filePath = "D:\FileLocation\file_name.csv"
(Get-Content $filePath | out-string).Replace('"', '""') | Set-Content $filePath
(Get-Content $filePath | out-string).Replace('|~|', '"') | Set-Content $filePath
(Get-Content $filePath | out-string).Replace('|#|', ',') | Set-Content $filePath
With such a large file, I suggest you process the file line by line (or in batches) which should speed up the entire process.
You can copy the Script mentioned by True here http://community.idera.com/powershell/ask_the_experts/f/learn_powershell-12/18821/how-to-remove-specific-rows-from-csv-files-in-powershell
but instead of writing $Line straight away, performing you replaces
$sw.WriteLine($line.replace().replace().replace())
Be careful with get-content since that will try to load the entire file and becomes very slow once you are out of memory.
Also be careful if you don't have much disk space. The linked solution will make a copy of the file (with the changes) before replacing it.
You can use -replace operator
$filepath="c:\temp\text.txt"
(Get-Content $filepath) -replace 'test','1' -replace 'text','2' -replace '123','3' |Set-Content $filepath
You can combine the .replace() in the same line.
$filepath="/Users/me/Desktop/text.txt"
'test text 123' |Out-File -Path $filepath
(Get-Content $filepath|Out-String).Replace('test','1').Replace('text','2').Replace('123','3')|Set-Content $filepath
Get-Content $filepath
1 2 3

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

How do I concatenate two text files in PowerShell?

I am trying to replicate the functionality of the cat command in Unix.
I would like to avoid solutions where I explicitly read both files into variables, concatenate the variables together, and then write out the concatenated variable.
Simply use the Get-Content and Set-Content cmdlets:
Get-Content inputFile1.txt, inputFile2.txt | Set-Content joinedFile.txt
You can concatenate more than two files with this style, too.
If the source files are named similarly, you can use wildcards:
Get-Content inputFile*.txt | Set-Content joinedFile.txt
Note 1: PowerShell 5 and older versions allowed this to be done more concisely using the aliases cat and sc for Get-Content and Set-Content respectively. However, these aliases are problematic because cat is a system command in *nix systems, and sc is a system command in Windows systems - therefore using them is not recommended, and in fact sc is no longer even defined as of PowerShell Core (v7). The PowerShell team recommends against using aliases in general.
Note 2: Be careful with wildcards - if you try to output to inputFiles.txt (or similar that matches the pattern), PowerShell will get into an infinite loop! (I just tested this.)
Note 3: Outputting to a file with > does not preserve character encoding! This is why using Set-Content is recommended.
Do not use >; it messes up the character encoding. Use:
Get-Content files.* | Set-Content newfile.file
In cmd, you can do this:
copy one.txt+two.txt+three.txt four.txt
In PowerShell this would be:
cmd /c copy one.txt+two.txt+three.txt four.txt
While the PowerShell way would be to use gc, the above will be pretty fast, especially for large files. And it can be used on on non-ASCII files too using the /B switch.
You could use the Add-Content cmdlet. Maybe it is a little faster than the other solutions, because I don't retrieve the content of the first file.
gc .\file2.txt| Add-Content -Path .\file1.txt
To concat files in command prompt it would be
type file1.txt file2.txt file3.txt > files.txt
PowerShell converts the type command to Get-Content, which means you will get an error when using the type command in PowerShell because the Get-Content command requires a comma separating the files. The same command in PowerShell would be
Get-Content file1.txt,file2.txt,file3.txt | Set-Content files.txt
I used:
Get-Content c:\FileToAppend_*.log | Out-File -FilePath C:\DestinationFile.log
-Encoding ASCII -Append
This appended fine. I added the ASCII encoding to remove the nul characters Notepad++ was showing without the explicit encoding.
If you need to order the files by specific parameter (e.g. date time):
gci *.log | sort LastWriteTime | % {$(Get-Content $_)} | Set-Content result.log
You can do something like:
get-content input_file1 > output_file
get-content input_file2 >> output_file
Where > is an alias for "out-file", and >> is an alias for "out-file -append".
Since most of the other replies often get the formatting wrong (due to the piping), the safest thing to do is as follows:
add-content $YourMasterFile -value (get-content $SomeAdditionalFile)
I know you wanted to avoid reading the content of $SomeAdditionalFile into a variable, but in order to save for example your newline formatting i do not think there is proper way to do it without.
A workaround would be to loop through your $SomeAdditionalFile line by line and piping that into your $YourMasterFile. However this is overly resource intensive.
To keep encoding and line endings:
Get-Content files.* -Raw | Set-Content newfile.file -NoNewline
Note: AFAIR, whose parameters aren't supported by old Powershells (<3? <4?)
I think the "powershell way" could be :
set-content destination.log -value (get-content c:\FileToAppend_*.log )