PowerShell Regex - How to remove everything after first line?



  • 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".



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



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

    sed ‘1!d’ file


  • 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.



  • @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



  • Could also do this one for Powershell.

    get-wmiobject -class win32_bios | Select serialnumber


  • Modified the title as this is specific to PowerShell.



  • 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
    


  • @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.