# Select-string

**URL:** <https://forums.powershell.org/t/select-string/6171>\
**Category:** PowerShell Help\
**Created:** [April 8, 2016, 9:14pm UTC](https://forums.powershell.org/t/select-string/6171 "2016-04-08T21:14:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vishal-joshi](https://avatars.discourse-cdn.com/v4/letter/v/fbc32d/32.png) [@vishal-joshi](https://forums.powershell.org/u/vishal-joshi)\
**Post date:** [April 8, 2016, 9:14pm UTC](https://forums.powershell.org/t/select-string/6171/1 "2016-04-08T21:14:20Z")

</div>

Hi All,

I Was trying to select a string using below code

$Finish1 = gci C:\DataWarehouseDump\*.txt | sort {$\_.LastWriteTime} | select -last 1 | select-string -pattern “Finished in [0-9]+ hours [0-9]+ minutes [0-9]+ seconds with [0-9]+ error(s), [0-9]+ warning(s) and [0-9]+ truncations”

And when i ran this code i got the below output

DataWarehouseDump20160309-1200.txt:462:09-03-2016 13:15:19 Finished in 1 hours 14 minutes 57 seconds with 0 error(s), 1 warning(s) and 1 truncations

But here i need the output without path and date, time like below

Finished in 1 hours 14 minutes 57 seconds with 0 error(s), 1 warning(s) and 1 truncations

to get this output i tried to split it  
$Finish3 = $finish1.tostring()  
$finish5 = $Finish3.split(“.”)[1]

Now i got this output  
txt:462:09-03-2016 13:15:19 Finished in 1 hours 14 minutes 57 seconds with 0 error(s), 1 warning(s) and 1 truncations  
but i dont want this text “txt:462:09-03-2016 13:15:19”

Please help me to get the desired output.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [April 8, 2016, 9:36pm UTC](https://forums.powershell.org/t/select-string/6171/2 "2016-04-08T21:36:04Z")

</div>

Select-String returns a MatchInfo object. Use `$Finish1.line` to access the result without the line number.

Edit:  
Sorry, just noticed you want to get rid of the date and time from the log file as well. Try this:  
`
$Finish1.line -replace “\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2} “,””
`

---

<div class="post-metadata">

**Author:** ![vishal-joshi](https://avatars.discourse-cdn.com/v4/letter/v/fbc32d/32.png) [@vishal-joshi](https://forums.powershell.org/u/vishal-joshi)\
**Post date:** [April 8, 2016, 10:10pm UTC](https://forums.powershell.org/t/select-string/6171/3 "2016-04-08T22:10:34Z")

</div>

Thank You it works.  
One More QQ.  
If i want to select output like below

in 1 hours 14 minutes 57 seconds with 0 error(s), 1 warning(s) and 1 truncations.

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [April 8, 2016, 10:40pm UTC](https://forums.powershell.org/t/select-string/6171/4 "2016-04-08T22:40:36Z")

</div>

This is an example of how it can work for you.

```
$string = 'DataWarehouseDump20160309-1200.txt:462:09-03-2016 13:15:19 Finished in 1 hours 14 minutes 57 seconds with 0 error(s), 1 warning(s) and 1 truncations'
If ($string -match "(?'output'Finished in (?'time'.*))") {
    [pscustomobject]@{Output = $Matches['output']
        Time = $Matches['time']}}

# Results
#Output : Finished in 1 hours 14 minutes 57 seconds with 0 error(s), 1 warning(s) and 1 truncations
#Time : 1 hours 14 minutes 57 seconds with 0 error(s), 1 warning(s) and 1 truncations
```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [April 8, 2016, 10:51pm UTC](https://forums.powershell.org/t/select-string/6171/5 "2016-04-08T22:51:51Z")

</div>

As you’re still working with the beginning of the string, just expand the replace:

```
$Finish1.line -replace "\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2} Finished ",""
```

Alternatively, if it’s always a fixed length, you could just select the sub-string:

```
$Finish1.line.Substring(29)
```

---

<div class="post-metadata">

**Author:** ![vishal-joshi](https://avatars.discourse-cdn.com/v4/letter/v/fbc32d/32.png) [@vishal-joshi](https://forums.powershell.org/u/vishal-joshi)\
**Post date:** [April 8, 2016, 11:20pm UTC](https://forums.powershell.org/t/select-string/6171/6 "2016-04-08T23:20:19Z")

</div>

Thanks Matt it worked

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:41pm UTC](https://forums.powershell.org/t/select-string/6171/7 "2024-05-16T20:41:38Z")

</div>


