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:\>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:\>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:\>$a,$b=get-content $OutFile 


Reference :-



About Prashanth Jayaram

DB Technologist, Author, Blogger, Service Delivery Manager at CTS, Automation Expert, Technet WIKI Ninja, MVB and Powershell Geek My Profile: https://social.technet.microsoft.com/profile/prashanth jayaram/ http://www.sqlshack.com/author/prashanth/ http://codingsight.com/author/prashanthjayaram/ https://www.red-gate.com/simple-talk/author/prashanthjayaram/ http://www.sqlservercentral.com/blogs/powersql-by-prashanth-jayaram/ Connect Me: Twitter @prashantjayaram GMAIL powershellsql@gmail.com The articles are published in: http://www.ssas-info.com/analysis-services-articles/ http://db-pub.com/ http://www.sswug.org/sswugresearch/community/
This entry was posted in PowerShell and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s