2
votes

What I am attempting to do is have an end CSV file like this:

path , get-childItem
path , get-childItem
path , get-childItem
etc.

I am very new to powershell so as of right now the issue I am having isn't with errors but rather actually knowing what to do

Here is my code thus far:

$data = gc "c:\Documents and Settings\user\Desktop\paths.txt"
ForEach($line in $data) {
$subName = Get-ChildItem "$line"
write-host $line","$subName
#Export-Csv -Path "C:\Documents and Settings\reidli\Desktop\mytest.csv" -Force "true" -InputObject $output 

So I am reading a list of paths from paths.txt where each line is a different path $subName is the list of Children i want and is correct and when I write-host the output is in fact: path,children commentented out is where I attempted to export csv, but this wont work as it would over-write the existing csv for each path

Can someone please help me take each line in the foreach loop and create a .csv file with each path and children?

2
Agreeing with PanikT's question below, what are you trying to accomplish, a line with 1337 filenames in the second column does not look very apitizing to me.Bas Bossink
Just to quickly follow up. I agree that a line with 1337 space separated file name doesn't look apealing. However, i know the files in question and what is behind them (no more than 4). The intention of this script isn't to determine what is there but rather simply create one file that lists them all. That way the script can be run at a later date to see if any updates were made. The output .csv was ugly has hell :) but exactly what i needed. So thanks PanikT!bananajunk

2 Answers

3
votes

just don't use export-csv at all. CSV is plain comma separated text file. which makes it easy to export:

$data = gc "c:\Documents and Settings\user\Desktop\paths.txt"
Remove-Item "C:\Documents and Settings\reidli\Desktop\mytest.csv" -ea 0
ForEach($line in $data) {
  $subName = Get-ChildItem "$line"
  write-host $line","$subName
  "$line,$subName" | Add-Content "C:\Documents and Settings\reidli\Desktop\mytest.csv" 
}

btw. do you really want the sub files space separated? it is what you get when you convert Get-ChildItem to string

0
votes

Try this:

$data | select $_ , @{L="subname";E=@{(gci $_)}} | Export-Csv "C:\Documents and Settings\reidli\Desktop\mytest.csv" -force