[How to] Select custom lines using PowerShell
On a daily basis work, we usually deal with multiple data files like(text, CSV, Xlsx Etc) with multiple lines of data as I do. When I moved on to the new team in my organization, I found that lot of manual work has to be done on a daily basis.
Working on a huge chunk of data on a daily basis I found that I had to select
If you are in an IT field, or you are an IT administrator, then definitely you have come across one or many alert monitoring systems like Moogsoft, SCOM etc. On the alerts, we had to select a few lines while we generate the reference ticket for the issue which we work and resolve. This has been a big manual task while we select data like HostNames, Alert Details, Descriptions, and a few other fields. Then we created this below script to minimize the manual work and save the time.
The idea behind this is, we have a file which contains the list of data and we need only a few lines to be copied.
Note: This script is not limited only for working on the alerting system or creating reference ticket. This is for all who works on the data files and needs only a few lines of data to work. If you are using any other methods like Excel Forms or Excel macros, believe me. This is better than that. G
- Copy and save the below Script as SelectCusotmLines.Ps1. and this script assumes that you have data is in the Desktop with the name Data.txt. You can always change it in the script.
- Run the Script from the PowerShell windows and that’s it. Your new selected data is automatically selected and copied for you.
- Open any text editor and paste the new data and use it as you need.
<#
.Synopsis
Minimize the manual work in selecting rows.
.Line14
Minimize the manual work in Selecting rows in a text file containing multiple lines and copy the content automatically.
.EXAMPLE
./SelectCusotmLines.Ps1
#>
#Script Name : SelectCusotmLines.Ps1
#Creator : Praveen Kumar
#Date : 16 Sep 2018
$a = (Get-Content "$HOME\Desktop\Data.txt")
$Line2 = $a | select -Skip 1 -First 1
$Line18 = $a | select -Skip 17 -First 1
$Line37 = $a | select -Skip 36 -First 1
$Line24 = $a | select -Skip 23 -First 1
$Line10 = $a | select -Skip 9 -First 1
$Line14 = $a | select -Skip 13 -First 1
#region Adding properties
$D = New-Object PSObject
$D | Add-Member NoteProperty -Name "SituationID" -Value $Line2
$D | Add-Member NoteProperty -Name "ENIVRONMENT" -Value $Line18
$D | Add-Member NoteProperty -Name "SERVICE" -Value $Line37
$D | Add-Member NoteProperty -Name "HOST" -Value $Line24
$D | Add-Member NoteProperty -Name "CLASS" -Value $Line10
$D | Add-Member NoteProperty -Name "DESCRIPTION" -Value $Line14
$D | Clip
#endregion
Below is the example of how this script works.
Above is the data which I want to customize and select a few properties. and when I execute the above script, I get the below result which is copied automatically and ready for me to use. I just used a text file to give you the output which is
I hope you enjoy this script and save your most precious time from this. Let me know how this helped you in your day to day work.
I was just searching for this info for some time. After 6 hours of continuous Googleing, at last I got it in your website. I wonder what’s the lack of Google strategy that don’t rank this type of informative sites in top of the list. Usually the top websites are full of garbage.