SOLVED

How to add Export-Csv to a script

Copper Contributor

Hello !

 

I'm studying PowerShell and I'd like to add an Export-Csv command for a script. However I couldn't understand how to put this command in it.

Here the script : 

 

$emplacement = "D:\DOCUMENTAIRE\"
$myFolders = @(
"File A",
"File B",
"File C",
"File D",
"File E",
"File F",
"File G",
"File H",
"File I",
"File J",
"File K",
"File L"
)
$nbDocuments=0

Write-Host "NUM;FOLDER;AGENCY;TYPE;DOMAIN;FILE"

For($i=0;$i -lt $myFolders.Length;$i++) 
{ 
    $files = Get-ChildItem -Path $emplacement$($myFolders[$i])
    For($j=0;$j -lt $files.Length;$j++) 
    { 
        $explode_content = $files[$j].ToString().Split("_")
        $nbDocuments++
        Write-Host "$nbDocuments;$($myFolders[$i]);$($explode_content[0]);$($explode_content[1]);$($explode_content[2]);$($files[$j])" 
        
    }
}
Write-Host "---------------------------------"
Write-Host "Total documents : $nbDocuments"
Write-Host "---------------------------------"

cmd / c pause

 

Thank you for your help !

 

Math

9 Replies

Hi @MP21260 

 

You just need to pipe it "|" to take the data and give it to the next command, in your case export-csv

 

example

 

$test = @(
"Test1",
"test2"
)

$test | export-csv C:\Users\Admin\Desktop\NameOfCSV.csv -delimiter ';'

Thats it.

 

Here is the official documentation:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powe...

Hello @Schnittlauch,

 

Thank you for replying ! I tried to put the export-csv  in the code. However it could work.

 

Perhaps something more is missing.

 

Here the modified code : 

 

$emplacement = "D:\DOCUMENTAIRE\"
$myFolders = @(
"File A",
"File B",
"File C",
"File D",
"File E",
"File F",
"File G",
"File H",
"File I",
"File J",
"File K",
"File L"
)
$nbDocuments=0

Write-Host "NUM;FOLDER;AGENCY;TYPE;DOMAIN;FILE"

For($i=0;$i -lt $myFolders.Length;$i++) 
{ 
    $files = Get-ChildItem -Path $emplacement$($myFolders[$i])
    For($j=0;$j -lt $files.Length;$j++) 
    { 
        $explode_content = $files[$j].ToString().Split("_")
        $nbDocuments++
        Write-Host "$nbDocuments;$($myFolders[$i]);$($explode_content[0]);$($explode_content[1]);$($explode_content[2]);$($files[$j])" 
        
    }
}

$files | export-csv C:\Users\Admin\Desktop\List.csv -delimiter ';'

Write-Host "---------------------------------"
Write-Host "Total documents : $nbDocuments"
Write-Host "---------------------------------"

cmd / c pause

Thank you again for your support.

 

Hi @MP21260
I'll be honest,
I have no idea what you are trying to do in line 18-30.
If you want, you can tell me the requirements you want to do with this script.
Best regards
Schnittlauch
Hi Schnittlauch,

sorry for my low knowledges.
When this command is launched in PowerShell, it gives a list of all files available in each folders.
With the export-csv command, I'd like to get all the files names in a file for Excel.

Thank you for your help again !

MP21260
best response confirmed by MP21260 (Copper Contributor)
Solution

Hello,

At the end of your for loop (within the loop) you would want to add your $file | export-csv... (line 32). As $files is being overwritten each time within the loop, you are going to only receive one output outside of the loop... Your last output which executes after your for loop.

I haven't tried your code personally, but typically when I export to CSV I create an object and export that as such.

 

 

$object = New-Object -TypeName psObject
$object | Add-member -MemberType NoteProperty -Name "Example" -value $variable
$object | Export-Csv -Path $logFile -Force -Encoding ASCII -Append -NoTypeInformation

 

 

Hello @Leavii,

thank you for your help ! I am about to solve the problem to create the CSV file.
I put this :

$object = New-Object -TypeName psObject
$object | Add-member -MemberType NoteProperty -Name "NUM;FOLDER;AGENCY;TYPE;DOMAIN;FILE" -value $files
$object | Export-Csv -Path C:\Users\Admin\Desktop\List.csv\Files_List.csv -Force -Encoding ASCII -Append -NoTypeInformation

However it shows no files inside Files_List.csv.
I can't understand why this happens.
Can you help again for this ?

Thank you a lot because It is very hard for me to understand.

Best regards,

MP21260

@MP21260 See if this is what you are trying to accomplish.  Change back to your paths.  I test on my computer and this works for listing files, and counting them.

 

 

$emplacement = "U:\files\scripts\ps1\test\" ##parent folder

$myFolders = @(  ##array
"File A",
"File B",
"File C",
"File D",
"File E",
"File F",
"File G",
"File H"
)

For($i=0;$i -lt $myFolders.Length;$i++) ## loop files in parent folder
{ 
    $files = Get-ChildItem -Path $emplacement$($myFolders[$i])  ##get files from array file 
    For($j=0;$j -lt $files.Length;$j++) #loop files in array file
    { 
        $file = $files[$j].Name #assign name of file
        $count++  #add to count of files in array of files

        $object = New-Object -TypeName psObject  #create object
        $object | Add-member -MemberType NoteProperty -Name "Count" -value $count ##add to object
        $object | Add-Member -MemberType NoteProperty -Name "File Name" -Value $file ## add to object
        $object | Export-Csv -Path files_list.csv -force -Encoding ASCII -Append -NoTypeInformation  ##export object
        
    }
}

Write-Host "---------------------------------"
Write-Host "Total documents : $count"
Read-Host "---------------------------------"  #read to pause script

 

@Leavii,

Thank you again for replying ! Finally it works perfectly.

Now I'm going to study this code to understand it better.
PowerShell is powerful and time to study it is required.

Thank you again !

Best regards,

MP21260
Glad I could help :)
1 best response

Accepted Solutions
best response confirmed by MP21260 (Copper Contributor)
Solution

Hello,

At the end of your for loop (within the loop) you would want to add your $file | export-csv... (line 32). As $files is being overwritten each time within the loop, you are going to only receive one output outside of the loop... Your last output which executes after your for loop.

I haven't tried your code personally, but typically when I export to CSV I create an object and export that as such.

 

 

$object = New-Object -TypeName psObject
$object | Add-member -MemberType NoteProperty -Name "Example" -value $variable
$object | Export-Csv -Path $logFile -Force -Encoding ASCII -Append -NoTypeInformation

 

 

View solution in original post