ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    PowerShell Regex - How to remove everything after first line?

    IT Discussion
    powershell regex computer name dell tag
    6
    9
    2.6k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • O
      Obsolesce
      last edited by scottalanmiller

      I have something like the following:

      First line of stuff
      
      
      
      
      

      There's a line of text, followed by a few blank lines.

      I'm looking for a regex to delete everything after the first line, leaving only:

      First line of stuff
      

      So far, I have found that

      ^(.*)
      

      selects only the first line. That doesn't work, i'm looking to use something like this in Powershell:

      $variable1 = $variableA -replace <regexStatement)

      Where, <regexStatement>,'' selects everything after the first line, and the comma and two tics after that replaces it with "nothing".

      1 Reply Last reply Reply Quote 1
      • O
        Obsolesce
        last edited by Obsolesce

        To simplify, all I need is a regex statement that selects everything after the 1st line. I got the rest.

        1 Reply Last reply Reply Quote 1
        • S
          stacksofplates
          last edited by

          Too bad it’s Powershell. I could help you with sed.

          sed ‘1!d’ file
          
          1 Reply Last reply Reply Quote 1
          • O
            Obsolesce
            last edited by Obsolesce

            I figured out a better way to do it, without using RegEx:

            $compName = Get-WmiObject win32_ComputerSystem | Select-Object -ExpandProperty Name
            if ($compName -match "WINDOWS") {
            $dellTag = Get-WmiObject win32_bios | Select-Object -ExpandProperty SerialNumber
            Rename-Computer -NewName $dellTag -Restart
            }
            

            What I was originally trying to do, I also figured out... by avoiding having to select new lines. I outputted the info to a text file first, and then I searched for a pattern, putting that into a variable, in which I could then just remove everything before the :<space>. Then I was left with the same result as above:

            $compName = Get-WmiObject win32_ComputerSystem | Select-Object -ExpandProperty Name
            if ($compName -match "WINDOWS") {
            $dtFile = "C:\dellTag.txt"
            $snList = Get-WmiObject win32_bios | FL SerialNumber
            $snList > $dtFile
            $dtFilter = Select-String -Pattern "SerialNumber" -Path $dtFile
            $dellTag = $dtFilter -replace '^[^_]*: ',''
            Remove-Item $dtFile
            Rename-Computer -NewName $dellTag -Restart
            }
            

            I got the RegEx idea from a previous script I made to comb through a log file. In that case I needed to use RegEx, but in this case, I could get what I needed more simply through PowerShell.

            D 1 Reply Last reply Reply Quote 1
            • D
              dbeato @Obsolesce
              last edited by dbeato

              @tim_g said in Regex - How to remove everything after first line?:

              I figured out a better way to do it, without using RegEx:

              $compName = Get-WmiObject win32_ComputerSystem | Select-Object -ExpandProperty Name
              if ($compName -match "WINDOWS") {
              $dellTag = Get-WmiObject win32_bios | Select-Object -ExpandProperty SerialNumber
              Rename-Computer -NewName $dellTag -Restart
              }
              

              What I was originally trying to do, I also figured out... by avoiding having to select new lines. I outputted the info to a text file first, and then I searched for a pattern, putting that into a variable, in which I could then just remove everything before the :<space>. Then I was left with the same result as above:

              $compName = Get-WmiObject win32_ComputerSystem | Select-Object -ExpandProperty Name
              if ($compName -match "WINDOWS") {
              $dtFile = "C:\dellTag.txt"
              $snList = Get-WmiObject win32_bios | FL SerialNumber
              $snList > $dtFile
              $dtFilter = Select-String -Pattern "SerialNumber" -Path $dtFile
              $dellTag = $dtFilter -replace '^[^_]*: ',''
              Remove-Item $dtFile
              Rename-Computer -NewName $dellTag -Restart
              }
              

              I got the RegEx idea from a previous script I made to comb through a log file. In that case I needed to use RegEx, but in this case, I could get what I needed more simply through PowerShell.

              SO you wanted to get the service tag correct? WIthout Powershell it would been
              wmic bios get serialnumber

              1 Reply Last reply Reply Quote 0
              • C
                coliver
                last edited by

                Could also do this one for Powershell.

                get-wmiobject -class win32_bios | Select serialnumber
                
                1 Reply Last reply Reply Quote 0
                • S
                  scottalanmiller
                  last edited by

                  Modified the title as this is specific to PowerShell.

                  1 Reply Last reply Reply Quote 0
                  • O
                    Obsolesce
                    last edited by Obsolesce

                    Actually, I'm sticking to this:

                    $compName = Get-WmiObject win32_ComputerSystem | Select-Object -ExpandProperty Name
                    if ($compName -match "WINDOWS") {
                    $dellTag = Get-WmiObject win32_bios | Select-Object -ExpandProperty SerialNumber
                    Rename-Computer -NewName $dellTag -Force -Restart
                    }
                    Exit
                    
                    T 1 Reply Last reply Reply Quote 1
                    • T
                      travisdh1 @Obsolesce
                      last edited by travisdh1

                      @tim_g said in PowerShell Regex - How to remove everything after first line?:

                      Actually, I'm sticking to this:

                      $compName = Get-WmiObject win32_ComputerSystem | Select-Object -ExpandProperty Name
                      if ($compName -match "WINDOWS") {
                      $dellTag = Get-WmiObject win32_bios | Select-Object -ExpandProperty SerialNumber
                      Rename-Computer -NewName $dellTag -Force -Restart
                      }
                      Exit
                      

                      Compared to sed, that makes my head hurt. Glad you got it sorted, and an example for when I'll need to do the same thing.

                      1 Reply Last reply Reply Quote 2
                      • 1 / 1
                      • First post
                        Last post