Solved

Remove content, while keeping first top line powershell

Posted on 2016-10-09
2
40 Views
Last Modified: 2016-10-09
I have a sample.txt file where I would like to remove the content, except for it first top line.

My sample.txt file looks like this:
1.dslkjfladsdjgmvjfgmldskbm;sldkvmg,;sdlmg;msj,;sdrl
2.dlkjfsadfjmsal;jsalv;dsvmdsfkgmrg,pvtpvhtphb[h.[y
3.fljsafckg,ksd,v;vyklt;vlkbmyulnmktr,ouf,f/.n,
4.dflcajsmglsdv;ks'ítb;pjk.'gpjnuk. uo.hulmk,vgjhumk.'l

I would like the output result to look like following:
1.dslkjfladsdjgmvjfgmldskbm;sldkvmg,;sdlmg;msj,;sdrl

I have tried these commands but output clears the entire file.
Clear-Content C:\sample.txt | where { $_.length -gt 2}| Set-Content C:\sample.txt

Clear-Content C:\sample.txt | Select -first 2 | Set-Content C:\sample.txt

Could someone please advise what am I missing?
Thanks
0
Comment
Question by:Maniman man
2 Comments
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 41836194
This is the fastest method that I'm aware of.  The -TotalCount parameter instructs it to just read the first line instead of the entire file.
(Get-Content C:\sample.txt -TotalCount 1) | Set-Content C:\sample.txt

Open in new window

0
 

Author Closing Comment

by:Maniman man
ID: 41836197
Thank you! It worked.
Best Regards
0

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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…

713 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