Monday, May 11, 2020

PowerShell File List to CSV


If you are looking at how to output specific file information to CSV, like File Extension or the Folder the file is in, you can use IO.FileInfo and IO.DirectoryInfo in PowerShell to achieve a specific result.


$parentFolder = "MyFolder"
$folder = "C:\Temp\" + $parentFolder

$files = Get-ChildItem -Path $folder -Recurse

foreach ($file in $files)
{
$name = ([IO.FileInfo] $file.FullName).Name
$parent = ([IO.DirectoryInfo] $file.FullName).Parent
$extension = ([IO.FileInfo] $file.FullName).Extension

$csvLine = """$parentFolder"",""$parent"",""$name"",""$extension"",""$directoryName"""
$outputFile = "C:\Temp\" + $parentFolder + ".csv"

if ($extension -ne ""){
Add-Content $outputFile $csvLine
}
}

Check This Out!

More Links to Good Information