Question

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?

Was it helpful?

Solution

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

OTHER TIPS

Try this:

$data | select $_ , @{L="subname";E=@{(gci $_)}} | Export-Csv "C:\Documents and Settings\reidli\Desktop\mytest.csv" -force
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top