Databases Reference
In-Depth Information
Notice that the contents of the
rnFile.txt
file includes what was written by
Set-Content
and
Add-Content
in Figure 5-25.
Figure 5-25
Get-Content
and
Set-Content
can also be used to merge files. Write a simple string to a new file called
file1.txt
with the
Set-Content
cmdlet. Then write a simple string to another new file called
file2.txt
.
You can use the
Get-Content
cmdlet to retrieve the contents of the two files as a string object, and pipe
the object to the
Set-Content
cmdlet to write to a new file,
file3.txt
. The complete commands are as
follows:
Set-Location C:\Windows\Temp\psDir
Set-Content file1.txt -Value "File 1
.
"
Set-Content file2.txt -Value "File 2
.
"
Get-Content file1.txt,file2.txt | Set-Content file3.txt
Get-Content file3.txt
As shown in Figure 5-26, the
Set-Content
cmdlet actually created
file1.txt
,
file2.txt
,and
file3.txt
.The
file3.txt
file contains the contents of both
file1.txt
and
file2.txt
.
Figure 5-26
To delete the contents of a file, such as its text, but not delete the item, use the
Clear-Content
cmdlet:
Clear-Content file3.txt
Get-Content file3.txt
Notice that the
file3.txt
file has been emptied in Figure 5-27.
Figure 5-27
Search WWH ::
Custom Search