Skip to main content
Corrected some broken code
Source Link
LotPings
  • 338
  • 1
  • 7

Using findstr in PowerShell is superfluous and
not very powershell'ish which is about objects and pipes.

You can directly pipe the raw output of Get-ChildItem to Select-String and parse the resulting object for the information you require.

As the size of the file isn't contained in the properties sls returns:

Context      Property
Filename     Property
IgnoreCase   Property
Line         Property
LineNumber   Property
Matches      Property
Path         Property
Pattern      Property

You've to append it, either with a calculated property

# Grab user options
$ext = Read-Host -Prompt        "Enter extension   "
$huntString = Read-Host -Prompt "Enter hunt string "

Get-ChildItem *.$ext -Recurse | Select-String $huntString -List | 
    Select-Object @{Name='Size';Expression=Label='Size';Expression={(Get-Item $_.Path).Length}},Path

or iterate the output and build a [PSCustomObject]:

Get-ChildItem *.$ext -Recurse | Select-String $huntString -List | 
    ForEach-Object {
        [PSCustomObject]@{
            Path = $_.Path
            Size = (Get-Item $_.path).Length
        }
    }

The objectsoutput will be the very same:

> Q:\Test\2018\12\17\CR_209811.ps1
Enter extension   : ps1
Enter hunt string : ::Now
Size Path
---- ----
 878 Q:\Test\2018\09\18\SO_52381514.ps1
 677 Q:\Test\2018\11\16\SO_53336923.ps1
 770 Q:\Test\2018\11\19\SO_53381881.ps1
1141 Q:\Test\2018\12\17\CR_209811.ps1
1259 Q:\Test\2018\12\17\SU_1385185.ps1

Using findstr in PowerShell is superfluous and
not very powershell'ish which is about objects and pipes.

You can directly pipe the raw output of Get-ChildItem to Select-String and parse the resulting object for the information you require.

As the size of the file isn't contained in the properties sls returns:

Context      Property
Filename     Property
IgnoreCase   Property
Line         Property
LineNumber   Property
Matches      Property
Path         Property
Pattern      Property

You've to append it, either with a calculated property

# Grab user options
$ext = Read-Host -Prompt        "Enter extension   "
$huntString = Read-Host -Prompt "Enter hunt string "

Get-ChildItem *.$ext -Recurse | Select-String $huntString -List | 
    Select-Object @{Name='Size';Expression={(Get-Item $_.Path).Length}},Path

or iterate the output and build a [PSCustomObject]:

Get-ChildItem *.$ext -Recurse | Select-String $huntString -List | 
    ForEach-Object {
        [PSCustomObject]@{
            Path = $_.Path
            Size = (Get-Item $_.path).Length
        }
    }

The objectsoutput will be the very same:

> Q:\Test\2018\12\17\CR_209811.ps1
Enter extension   : ps1
Enter hunt string : ::Now
Size Path
---- ----
 878 Q:\Test\2018\09\18\SO_52381514.ps1
 677 Q:\Test\2018\11\16\SO_53336923.ps1
 770 Q:\Test\2018\11\19\SO_53381881.ps1
1141 Q:\Test\2018\12\17\CR_209811.ps1
1259 Q:\Test\2018\12\17\SU_1385185.ps1

Using findstr in PowerShell is superfluous and
not very powershell'ish which is about objects and pipes.

You can directly pipe the raw output of Get-ChildItem to Select-String and parse the resulting object for the information you require.

As the size of the file isn't contained in the properties sls returns:

Context      Property
Filename     Property
IgnoreCase   Property
Line         Property
LineNumber   Property
Matches      Property
Path         Property
Pattern      Property

You've to append it, either with a calculated property

# Grab user options
$ext = Read-Host -Prompt        "Enter extension   "
$huntString = Read-Host -Prompt "Enter hunt string "

Get-ChildItem *.$ext -Recurse | Select-String $huntString -List | 
    Select-Object @{Label='Size';Expression={(Get-Item $_.Path).Length}},Path

or iterate the output and build a [PSCustomObject]:

Get-ChildItem *.$ext -Recurse | Select-String $huntString -List | 
    ForEach-Object {
        [PSCustomObject]@{
            Path = $_.Path
            Size = (Get-Item $_.path).Length
        }
    }

The objectsoutput will be the very same:

> Q:\Test\2018\12\17\CR_209811.ps1
Enter extension   : ps1
Enter hunt string : ::Now
Size Path
---- ----
 878 Q:\Test\2018\09\18\SO_52381514.ps1
 677 Q:\Test\2018\11\16\SO_53336923.ps1
 770 Q:\Test\2018\11\19\SO_53381881.ps1
1141 Q:\Test\2018\12\17\CR_209811.ps1
1259 Q:\Test\2018\12\17\SU_1385185.ps1

Using findstr in PowerShell is superfluous and
not very powershell'ish which is about objects and pipes.

You can directly pipe the raw output of Get-ChildItem to Select-String and parse the resulting object for the information you require.

As the size of the file isn't contained in the properties sls returns:

Context      Property
Filename     Property
IgnoreCase   Property
Line         Property
LineNumber   Property
Matches      Property
Path         Property
Pattern      Property

You've to append it, either with a calculated property

# Grab user options
$ext = Read-Host -Prompt        "Enter extension   "
$huntString = Read-Host -Prompt "Enter hunt string "

Get-ChildItem *.$ext -Recurse | Select-String $huntString -List | 
    Select-Object @{Label='Size';Expression=Name='Size';Expression={(Get-Item $_.Path).Length}},Path

or iterate the output and build a [PSCustomObject]:

Get-ChildItem *.$ext -Recurse | Select-String $huntString -List | 
    ForEach-Object {
        [PSCustomObject]@{
            Path = $_.Path
            Size = (Get-Item $_.path).Length
        }
    }

The objectsoutput will be the very same:

> Q:\Test\2018\12\17\CR_209811.ps1
Enter extension   : ps1
Enter hunt string : ::Now
Size Path
---- ----
 878 Q:\Test\2018\09\18\SO_52381514.ps1
 677 Q:\Test\2018\11\16\SO_53336923.ps1
 770 Q:\Test\2018\11\19\SO_53381881.ps1
1141 Q:\Test\2018\12\17\CR_209811.ps1
1259 Q:\Test\2018\12\17\SU_1385185.ps1

Using findstr in PowerShell is superfluous and
not very powershell'ish which is about objects and pipes.

You can directly pipe the raw output of Get-ChildItem to Select-String and parse the resulting object for the information you require.

As the size of the file isn't contained in the properties sls returns:

Context      Property
Filename     Property
IgnoreCase   Property
Line         Property
LineNumber   Property
Matches      Property
Path         Property
Pattern      Property

You've to append it, either with a calculated property

# Grab user options
$ext = Read-Host -Prompt        "Enter extension   "
$huntString = Read-Host -Prompt "Enter hunt string "

Get-ChildItem *.$ext -Recurse | Select-String $huntString -List | 
    Select-Object @{Label='Size';Expression={(Get-Item $_.Path).Length,Path

or iterate the output and build a [PSCustomObject]:

Get-ChildItem *.$ext -Recurse | Select-String $huntString -List | 
    ForEach-Object {
        [PSCustomObject]@{
            Path = $_.Path
            Size = (Get-Item $_.path).Length
        }
    }

The objectsoutput will be the very same:

> Q:\Test\2018\12\17\CR_209811.ps1
Enter extension   : ps1
Enter hunt string : ::Now
Size Path
---- ----
 878 Q:\Test\2018\09\18\SO_52381514.ps1
 677 Q:\Test\2018\11\16\SO_53336923.ps1
 770 Q:\Test\2018\11\19\SO_53381881.ps1
1141 Q:\Test\2018\12\17\CR_209811.ps1
1259 Q:\Test\2018\12\17\SU_1385185.ps1

Using findstr in PowerShell is superfluous and
not very powershell'ish which is about objects and pipes.

You can directly pipe the raw output of Get-ChildItem to Select-String and parse the resulting object for the information you require.

As the size of the file isn't contained in the properties sls returns:

Context      Property
Filename     Property
IgnoreCase   Property
Line         Property
LineNumber   Property
Matches      Property
Path         Property
Pattern      Property

You've to append it, either with a calculated property

# Grab user options
$ext = Read-Host -Prompt        "Enter extension   "
$huntString = Read-Host -Prompt "Enter hunt string "

Get-ChildItem *.$ext -Recurse | Select-String $huntString -List | 
    Select-Object @{Name='Size';Expression={(Get-Item $_.Path).Length}},Path

or iterate the output and build a [PSCustomObject]:

Get-ChildItem *.$ext -Recurse | Select-String $huntString -List | 
    ForEach-Object {
        [PSCustomObject]@{
            Path = $_.Path
            Size = (Get-Item $_.path).Length
        }
    }

The objectsoutput will be the very same:

> Q:\Test\2018\12\17\CR_209811.ps1
Enter extension   : ps1
Enter hunt string : ::Now
Size Path
---- ----
 878 Q:\Test\2018\09\18\SO_52381514.ps1
 677 Q:\Test\2018\11\16\SO_53336923.ps1
 770 Q:\Test\2018\11\19\SO_53381881.ps1
1141 Q:\Test\2018\12\17\CR_209811.ps1
1259 Q:\Test\2018\12\17\SU_1385185.ps1
Source Link
LotPings
  • 338
  • 1
  • 7

Using findstr in PowerShell is superfluous and
not very powershell'ish which is about objects and pipes.

You can directly pipe the raw output of Get-ChildItem to Select-String and parse the resulting object for the information you require.

As the size of the file isn't contained in the properties sls returns:

Context      Property
Filename     Property
IgnoreCase   Property
Line         Property
LineNumber   Property
Matches      Property
Path         Property
Pattern      Property

You've to append it, either with a calculated property

# Grab user options
$ext = Read-Host -Prompt        "Enter extension   "
$huntString = Read-Host -Prompt "Enter hunt string "

Get-ChildItem *.$ext -Recurse | Select-String $huntString -List | 
    Select-Object @{Label='Size';Expression={(Get-Item $_.Path).Length,Path

or iterate the output and build a [PSCustomObject]:

Get-ChildItem *.$ext -Recurse | Select-String $huntString -List | 
    ForEach-Object {
        [PSCustomObject]@{
            Path = $_.Path
            Size = (Get-Item $_.path).Length
        }
    }

The objectsoutput will be the very same:

> Q:\Test\2018\12\17\CR_209811.ps1
Enter extension   : ps1
Enter hunt string : ::Now
Size Path
---- ----
 878 Q:\Test\2018\09\18\SO_52381514.ps1
 677 Q:\Test\2018\11\16\SO_53336923.ps1
 770 Q:\Test\2018\11\19\SO_53381881.ps1
1141 Q:\Test\2018\12\17\CR_209811.ps1
1259 Q:\Test\2018\12\17\SU_1385185.ps1