ForEach command with Get-childItem error

I’m hoping for any assistance with this script. The script below need to to the following:

  1. read the folder in the $DRIVEPATH variable and create a list of sub-folder name in a CSV file, which the script does create correctly
  2. Then use the CSV file to run a command (ForEach ($proj in $projlist) line then apply each folder name to the sub-command (Get-ChildItem -Path $proj -Recurse -File |…) line, injecting each line from the CVS file and log the last modified file in the a new CSV file for report.

$drivePath = “c:\temp\test”
$reportdate = Get-Date -Format “MM-dd-yy”
$serverpath = $drivePath.trimstart("\")
$servpath = split-path c:$serverpath -leaf

get-childitem -path $drivePath -Directory | select fullName | export-csv c:\temp\projlist.csv -NoTypeInformation
$projlist = Get-Content -Path c:\temp\projlist.csv
ForEach ($proj in $projlist) {Get-ChildItem -Path $proj -Recurse -File | Where-Object {($.LastWriteTime -gt (Get-Date).AddMonths(-1) )} | Sort-Object -Descending -Property LastWriteTime | select -First 1 FullName, LastWriteTime | Export-Csv "c:\temp\Report$servpath.csv" -append -NoTypeInformation }

When I run this script, I’m getting the following error:
Get-ChildItem : Illegal characters in path.
At C:\Users\tett\Documents\Test_script-SP.PS1:14 char:1

  • Get-ChildItem -Path $proj -Recurse -File | Where-Object {($_.LastWrit …
  •   + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], ArgumentException
      + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.GetChildItemCommand
    
    

Get-ChildItem : A parameter cannot be found that matches parameter name ‘File’.
At C:\Users\tett\Documents\Test_script-SP.PS1:14 char:36

  • Get-ChildItem -Path $proj -Recurse -File | Where-Object {($_.LastWrit …

any assistance with this script would be greatly appreciated and thanks in-advance.

sparkcad,
Welcome to the forum. :wave:t4:

Actually the intermediate CSV file is not necessary. This should be all you need.

$drivePath  = 'c:\temp\test'
$reportdate = Get-Date -Format 'yyyy-MM-dd'
$PathInfo   = Split-Path -Path $drivePath -Leaf
$outputPath = "c:\temp\Report_$($reportdate)_$($PathInfo).csv"
$OneMonthBefore = (Get-Date).AddMonths(-1)

Get-ChildItem -Path $drivePath -Directory |
    ForEach-Object {
        Get-ChildItem -Path $_.FullName -Recurse -File |
            Where-Object { $_.LastWriteTime -gt $OneMonthBefore  } |
                Sort-Object -Property LastWriteTime -Descending |
                    Select-Object -First 1 -Property FullName, LastWriteTime 
    }|
        Export-Csv -Path $outputPath -NoTypeInformation

Please, next time you post code or error messages format it as code using the preformatted text button ( </> ). Place your cursor on an empty line, click the button and paste the code or the error message.

Thanks in advance.

1 Like

Thank you so much, wasn’t expecting so quick response. removing the intermediate CSV does make it easier. really appreciate for the assistance. Also thank you for the advice on posting protocol. will follow the protocol next time. thanks again