Working with a text file or text format in PowerShell is a
good thing to learn.
Getting or monitoring output from PowerShell for further
processing or analysis and having it in text file format can easily be imported
to other applications.
Working with text or other format, requires these basic
operations: Save, Overwrite, Append and Save As.
Save, overwrite, append and save as is a common operation
when editing word documents, excel file and other office applications since it’s
freely available on the menu of the application. But can we do this in
PowerShell? The answer is a resounding, yes.
So, how to do it? Examples below shows how to work with text
files in PowerShell and do the operation mentioned above.
Equivalent operations for Save, Overwrite, Append and Save
As in PowerShell:
===Set-content ==== will overwrite the file
===Add-content ==== will just add or append something to the
file
===Out-File ==== is like "Save As" since you have
the option to save the file to another file
===Out-File ==== can also work as “Save” if the file does
not exist then it will be created
=== > (redirection operator) === also work as “save” the output
can be saved or redirected to a file
Scripting examples:
“Save As” example by saving the file to another file name so
the previous file remain untouched.
(Get-Content c:\2018_file.txt) | Foreach-Object {$_ -replace
'\[2018\]','[2019]'} | Out-File 2019_file.txt
2018_file.txt should have this format:
Jan [2018]
Feb [2018]
Mar [2018]
2019_file.txt will have this output:
Jan [2019]
Feb [2019]
Mar [2019]
Appending a text or string to a file:
Add-Content c:\2019_file.txt "Apr [2019]"
The above command will add the string “Apr [2019]” to the
end of the file, output will be like this:
Jan [2019]
Feb [2019]
Mar [2019]
Apr [2019]
Overwriting a text file the Set-Content function can be
used, example below will overwrite the text file 2019_file.txt
Set-Content -Path d:\2019_file.txt -Value 'Hello, Overwrite
World'
Output or the contents of the file will be:
Hello, Overwrite World
Saving the file can be done using the Out-File function or
the “>” redirection operator.
Gci d:\*.xml | Select-Object -ExpandProperty Name > d:\xmlfiles.txt
Above command will search or get all the name of the xml
files in D and save the output to xmlfiles.txt
Saving the output file can be done also as:
ls d:\ | % Name | out-file d:\dfiles.txt
Above command will read all files and folders in D drive. Only
the file in D it won’t include sub-folders or files in sub-directories.
That’s it. Working with text files in PowerShell is easy and
fun.
Cheers! Till next time...
================================
Free Android Apps:
Click links below to find out more:
Excel Keyboard guide:
Heaven's Dew Fall Prayer app for Android :
Catholic Rosary Guide for Android:
Pray the Rosary every day, countless blessings will be showered upon your life if you recite the Rosary faithfully.
https://play.google.com/store/apps/details?id=com.myrosaryapp
https://play.google.com/store/apps/details?id=com.myrosaryapp
Comments
Post a Comment