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/

 

Advertisements

About Prashanth Jayaram

I’m a Database technologist having 11+ years of rich, hands-on experience on Database technologies. I am Microsoft Certified Professional and backed with a Degree in Master of Computer Application. My expertise lies in T-SQL programming, Replication and PowerShell You can connect me via https://social.technet.microsoft.com/profile/prashanth jayaram/ http://www.sqlshack.com/author/prashanth/ https://www.tumblr.com/blog/prashantjayaram http://www.sqlservercentral.com/blogs/powersql-by-prashanth-jayaram/ http://www.toadworld.com/members/prashanthjayaram/ My Articles are published in following sites 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 )

Google+ photo

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

Connecting to %s