Solved

Remove content, while keeping first top line powershell

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now