none
remove the top line of a txt file powershell

    Question

  • i have been almost an hour trying to figure out how to remove the top line of a txt file with power shell.

    I have exported my computernames in my ou to a text file and removed the quotations but the top header line i cant make go away.

    Any suggetions.

     $AllObj | ForEach-Object {
      try {
        New-Object PSObject -Property @{
          CN   = $($_.Properties.cn);
        }
      }
      catch { } #Don't do anything - just ignore it

    } | select-object CN | `Export-csv -path $UserInfoFile -NoTypeInformation

     


    ronald white
    Monday, December 12, 2011 7:55 PM

Answers

  • i have been almost an hour trying to figure out how to remove the top line of a txt file with power shell.

     

     


    ronald white

    To remove the top line, one way is this:

    Get-Content file1.txt | Select-Object -Skip 1 | Out-File file2.txt
    



    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
    • Marked as answer by tnetplus Monday, December 12, 2011 8:26 PM
    Monday, December 12, 2011 8:22 PM
  • Hi,

    get-content myfile.txt | select-object -skip 1

    Bill

    • Marked as answer by tnetplus Monday, December 12, 2011 8:28 PM
    Monday, December 12, 2011 8:22 PM

All replies

  • $AllObj | Select -ExpandProperty CN | Export-Csv $userinfofile -NoTypeInformation
    
    or
    
    } | select-object -ExpandProperty CN | Export-Csv $userinfofile -NoTypeInformation
    

    You can either use the first line by itself or use the second line to replace your last line (depends on what $AllObj contains).
    Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP

    Engineering Efficiency
    @Rich_Prescott
    Client System Administration tool
    AD User Creation tool
    Monday, December 12, 2011 8:00 PM
  • $AllObj | Select -ExpandProperty CN | Export-Csv $userinfofile -NoTypeInformation
    
    or
    
    } | select-object -ExpandProperty CN | Export-Csv $userinfofile -NoTypeInformation
    

    You can either use the first line by itself or use the second line to replace your last line (depends on what $AllObj contains).
    Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP

    Engineering Efficiency
    @Rich_Prescott
    Client System Administration tool
    AD User Creation tool


    this is what it produces

     

    Length
    15
    15
    15
    15
    15
    15
    15
    15
    15
    15
    15


    ronald white
    Monday, December 12, 2011 8:09 PM
  • Hi,

    You need to post what your file looks like.

    Bill

    Monday, December 12, 2011 8:15 PM
  • CN
    KSEMTRANDCVTR01
    KSEMHETC67VC1WN
    KSEMWRHSDCV3SUK
    KSEMTRAN50V7541
    KSEMCSRC73V6384
    KSEMCSRC73V0882
    KSEMTRANDCVHMA8
    KSEMENGN67VAKVE
    ronald white
    Monday, December 12, 2011 8:17 PM
  • Hi,

    get-content myfile.txt | select-object -skip 1

    Bill

    • Marked as answer by tnetplus Monday, December 12, 2011 8:28 PM
    Monday, December 12, 2011 8:22 PM
  • i have been almost an hour trying to figure out how to remove the top line of a txt file with power shell.

     

     


    ronald white

    To remove the top line, one way is this:

    Get-Content file1.txt | Select-Object -Skip 1 | Out-File file2.txt
    



    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
    • Marked as answer by tnetplus Monday, December 12, 2011 8:26 PM
    Monday, December 12, 2011 8:22 PM
  • Thanks SO SO much

     

    God help me i am going to take my best batch and biggest i ever put together and somehow make it for Power shell.  This will no be easy. 


    ronald white
    Monday, December 12, 2011 8:28 PM
  • Here's one way to remove the first line from a text files:

     

    get-content test.txt
    
    "****************"
    
    $text = {gc test.txt}.invoke()
    $text.removeat(0)
    $text
    

     

     

    CN
     KSEMTRANDCVTR01
     KSEMHETC67VC1WN
     KSEMWRHSDCV3SUK
     KSEMTRAN50V7541
     KSEMCSRC73V6384
     KSEMCSRC73V0882
     KSEMTRANDCVHMA8
     KSEMENGN67VAKVE
    ****************
     KSEMTRANDCVTR01
     KSEMHETC67VC1WN
     KSEMWRHSDCV3SUK
     KSEMTRAN50V7541
     KSEMCSRC73V6384
     KSEMCSRC73V0882
     KSEMTRANDCVHMA8
     KSEMENGN67VAKVE

     


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Monday, December 12, 2011 8:40 PM
  • You can also create the csv file with no header information, thus saving a step:

    http://unlockpowershell.wordpress.com/2009/12/15/powershell-export-csv-with-no-header/

    Karl


    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Tuesday, December 13, 2011 6:15 PM
  • Hi,

    Can you help me with a comm that removes the last rows in a .txt file.

    I`m generating it and I allways get 2-3 empty row at the end.

    Thanks.

    Tuesday, March 27, 2012 5:37 AM
  • Hi Daniel,

    Do you mind starting a new thread / topic / question for this?  This thread has been closed for some time, and it's unlikely many people will look at it.


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Tuesday, March 27, 2012 5:56 AM