Reputation: 1454
Is there a way using PowerShell Out-File that for each line inserted into the file to add a * at the beginning and end of each text in the Out-File?
Example: Get-Process | Out-File -FilePath .\Process.txt
Test
Test1
Example Process.txt should contain:
*Test*
*Test1*
Upvotes: 0
Views: 264
Reputation: 25001
If you have a collection, which is the output of most commands, you can loop through each element and format the output string. The example below uses the (-f
) format operator to build the string.
$collection = @('test','test1','test2')
foreach ($entry in $collection) {
"*{0}*" -f $entry | Out-File .\Process.txt -Append
}
The case above is simplistic. However, when outputting from a command, you typically get an object. You will need to tweak this to select the object properties you care about. If we adapt the above example for Get-Process
, you can do the following:
foreach ($name in (Get-Process).Name) {
"*{0}*" -f $name | Out-File .\Process.txt -Append
}
A conceptually similar but less verbose option with aliasing that only writes once to the output file:
Get-Process |% {"*$($_.Name)*"} | Out-File .\Process.txt
Upvotes: 1