Solved

Remove content, while keeping first top line powershell

Posted on 2016-10-09
2
46 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 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