Powershell to truncate string in text

Posted on 2016-08-05
Last Modified: 2016-08-11
I have a powershell script below use to truncate the string in the string_output2.txt however the script below doesn't work, what I would need to output is something like
this save in string_output22.txt
Get-Content (Get-ChildItem F:\Powershell\string_output2.txt) | % {$_.Split(':')[1]} | Out-File F:\Powershell\string_output22.txt

Here is contain in string_output2.txt
Question by:motioneye
  • 3
  • 3
LVL 84

Expert Comment

ID: 41743998
You don't need the "Get-ChildItem" since you're only addressing a single file, you need to use $_.Split(':')[2], and if you want to to overwrite the source file, you need to put Get-Content in parenthesis, so that PS will read the whole file before continuing with the pipeline.
(Get-Content F:\Powershell\string_output2.txt) | % {$_.Split(':')[2]} | Out-File F:\Powershell\string_output22.txt

Open in new window


Author Comment

ID: 41744540
The ouput return me with null, its strange though to see teh output like this.

LVL 84

Expert Comment

ID: 41744548
That's a Unicode; use Out-File's -Encoding argument:
(Get-Content F:\Powershell\string_output2.txt) | % {$_.Split(':')[2]} | Out-File F:\Powershell\string_output22.txt -Encoding ASCII

Open in new window

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!


Author Comment

ID: 41744571
Hi oBdA
Yes I have tried that, still I have the same error as per screenshot
LVL 84

Accepted Solution

oBdA earned 500 total points
ID: 41744702
Get-Content F:\Powershell\string_output2.txt

Open in new window

print the expected strings?
Since you're replacing the original file, have you restored string_output2.txt to one that was created by the original source?
You can try Set-Content instead of Out-File as well.

Author Closing Comment

ID: 41751635
Hi oBdA,
Thanks for help, I have to copy the text from one file and move to another new file, then the scripts worked with no flaw. thanks again for the assistance and the opinion

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
In this previous article (, we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question