Blog Post

PowerSehll : How to suppress the table heading and remove the blank space from a text file

,

There are a multiple ways to suppress and remove the first line from text file. The first scenario would be writing a suppressed heading to an output text file Or after writing, we can remove the line using trim function Or using the trick shared in PowerShell magazine.

Just give you a background, we wanted to create a input a file based on some criteria and use the output  text file as an input for further processing.

For example,

I came across an requirement to uninstall the list of patches that were installed yesterday. Have to get a list of patches and store it in a file and used PowerShell script to uninstall the patches.

Using Looping

PS:\>$Computername='ABCD'
PS:\>$outFile='HotFix.txt'
PS:\>Get-HotFix -ComputerName $computerName|
where {$_.installedOn -gt "4/01/2013 12:00:00 AM "}|
ForEach-Object { $_.hotfixID >>$outFile}

Using Trim and set-content cmdlet to update the file by trimming the blank space

 

PS:\>$Computername='ABCD'
PS:\>$outFile='HotFix.txt'
PS:\>Get-HotFix -ComputerName $computername|where {$_.installedOn -gt "4/01/2013 12:00:00 AM "}|select hotFixID |out-file $outFile
PS:\>(gc $outFile) | ? {$_.trim() -ne "" } | set-content $OutFile

Using assignment and and redirecting the operator

PS:\>$Computername='ABCD'
PS:\>$outFile='HotFix.txt'
PS:\>$a,$b=get-content $OutFile 
PS:\>$b>$OutFile

removeBlank

Reference :-

remove-the-first-line-from-a-text-file/

 

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating