Forum Discussion

Fred_Elmendorf's avatar
Fred_Elmendorf
Brass Contributor
Jan 06, 2023
Solved

formatting output as a .csv file

This is my first attempt at PowerShell, and I have gathered lots of helpful information from existing forum threads and documentation, but I'm still not getting what I need. I need the latest *.pqd f...
  • Harm_Veenstra's avatar
    Harm_Veenstra
    Jan 09, 2023

    Fred_Elmendorf Modified it again, script is like this now:

     

    $path='d:\temp'
    $total = ForEach ($dir in Get-ChildItem $path | Where-Object PSIsContainer -eq $True ) {
        $files = Get-ChildItem "$($path)\$($dir.name)" -Recurse | Where-Object { ($_.name -Like "trend*.pqd") -and ($_.PSIsContainer -eq $False) } 
        if ($null -ne $files) {
            foreach ($file in $files) {
                [PSCustomObject]@{
                    Folder        = $file.DirectoryName.Split('\')[-3..-1] -join '\'
                    Name          = $file.Name
                    LastWriteDate = $file.LastWriteTime.ToString().Split(' ')[0]
                    LastWriteTime = $file.LastWriteTime.ToString().Split(' ')[1]
                }
            }
        }
    }
    
    $total | Export-Csv -Path d:\temp\report.csv -NoTypeInformation -Encoding UTF8 -Delimiter ','

     

    Output is like this:

     

    "Folder","Name","LastWriteDate","LastWriteTime"
    "D:\temp\x2","trend123.pqd","8-1-2023","22:39:18"
    "D:\temp\x3","trend123456.pqd","8-1-2023","22:48:44"
    "x5\x6\red","trendabcdef.pqd","9-1-2023","21:40:32"

     

    The last is inside a deeper path than the rest. Perhaps you should tweak the .Split numbers a bit, in my case the full path of the last item is D:\Temp\x4\x5\x6\red.

     

    There's a link beneath this post, mark as best response 😉 

Resources