Solved

Remove content, while keeping first top line powershell

Posted on 2016-10-09
2
36 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 39

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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