Skip to content

Instantly share code, notes, and snippets.

@tylerlmz1
Created April 7, 2020 22:04
Show Gist options
  • Save tylerlmz1/7496e30b0354565a5f03f9fc669f3780 to your computer and use it in GitHub Desktop.
Save tylerlmz1/7496e30b0354565a5f03f9fc669f3780 to your computer and use it in GitHub Desktop.
extract specified line range from a text file to a new text file
# get source and destination file name
$fileName = Read-Host -Prompt 'Source file name'
$content = Get-Content $fileName
$newFileName = Read-Host -Prompt 'New file name'
# get line number
$start = Read-Host -Prompt 'Starting line number'
$end = Read-Host -Prompt 'Ending line number'
# write target lines from source file into destination file
Get-Content -Path $fileName | Select-Object -Index ($start..$end) | Foreach-Object {
Add-Content "./$newFileName" $PSItem
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment