Skip to main content

Posts

Showing posts from August, 2013

PowerShell Find specific text within a string, with a list of partial file names. $_.split

Recently i was looking for a way to select all the text between two brackets and ignore everything else in the string.  This was necessary as i had a list of partial file name and i wanted a way of matching my list with a directory listing. The use of '$_.split' command achieved this very nicely. How to i select specific text in a string? $text='this is an (apple). it is red' #The variable $text contains the word 'apple' between two opposite brackets. $text|%{$_.split('(')[1]}|%{$_.split(')')[0]} # $text is piped into a foreach loop spliting the string, removing everything before the first bracket (. It is then piped into a second foreach loop removing everything after the second bracket ).                                    apple # On screen the returned result is apple. This is useful if you are looking for partial file names, or a specific string within a string. Wh

PowerShell Find specific text within a string, with a list of partial file names. $_.split

Recently i was looking for a way to select all the text between two brackets and ignore everything else in the string.  This was necessary as i had a list of partial file name and i wanted a way of matching my list with a directory listing. The use of '$_.split' command achieved this very nicely. How to i select specific text in a string? $text='this is an (apple). it is red' #The variable $text contains the word 'apple' between two opposite brackets. $text|%{$_.split('(')[1]}|%{$_.split(')')[0]} # $text is piped into a foreach loop spliting the string, removing everything before the first bracket (. It is then piped into a second foreach loop removing everything after the second bracket ).                                    apple # On screen the returned result is apple. This is useful if you are looking for partial file names, or a specific strin