SOLVED

How to add Export-Csv to a script

%3CLINGO-SUB%20id%3D%22lingo-sub-3104804%22%20slang%3D%22en-US%22%3EHow%20to%20add%20Export-Csv%20to%20a%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3104804%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20studying%20PowerShell%20and%20I'd%20like%20to%20add%20an%20Export-Csv%20command%20for%20a%20script.%20However%20I%20couldn't%20understand%20how%20to%20put%20this%20command%20in%20it.%3C%2FP%3E%3CP%3EHere%20the%20script%20%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24emplacement%20%3D%20%22D%3A%5CDOCUMENTAIRE%5C%22%0A%24myFolders%20%3D%20%40(%0A%22File%20A%22%2C%0A%22File%20B%22%2C%0A%22File%20C%22%2C%0A%22File%20D%22%2C%0A%22File%20E%22%2C%0A%22File%20F%22%2C%0A%22File%20G%22%2C%0A%22File%20H%22%2C%0A%22File%20I%22%2C%0A%22File%20J%22%2C%0A%22File%20K%22%2C%0A%22File%20L%22%0A)%0A%24nbDocuments%3D0%0A%0AWrite-Host%20%22NUM%3BFOLDER%3BAGENCY%3BTYPE%3BDOMAIN%3BFILE%22%0A%0AFor(%24i%3D0%3B%24i%20-lt%20%24myFolders.Length%3B%24i%2B%2B)%20%0A%7B%20%0A%20%20%20%20%24files%20%3D%20Get-ChildItem%20-Path%20%24emplacement%24(%24myFolders%5B%24i%5D)%0A%20%20%20%20For(%24j%3D0%3B%24j%20-lt%20%24files.Length%3B%24j%2B%2B)%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%24explode_content%20%3D%20%24files%5B%24j%5D.ToString().Split(%22_%22)%0A%20%20%20%20%20%20%20%20%24nbDocuments%2B%2B%0A%20%20%20%20%20%20%20%20Write-Host%20%22%24nbDocuments%3B%24(%24myFolders%5B%24i%5D)%3B%24(%24explode_content%5B0%5D)%3B%24(%24explode_content%5B1%5D)%3B%24(%24explode_content%5B2%5D)%3B%24(%24files%5B%24j%5D)%22%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%7D%0AWrite-Host%20%22---------------------------------%22%0AWrite-Host%20%22Total%20documents%20%3A%20%24nbDocuments%22%0AWrite-Host%20%22---------------------------------%22%0A%0Acmd%20%2F%20c%20pause%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20your%20help%20!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMath%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3104804%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Ecommunity%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20Server%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3135251%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20add%20Export-Csv%20to%20a%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3135251%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1294606%22%20target%3D%22_blank%22%3E%40MP21260%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20just%20need%20to%20pipe%20it%20%22%7C%22%20to%20take%20the%20data%20and%20give%20it%20to%20the%20next%20command%2C%20in%20your%20case%20export-csv%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eexample%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24test%20%3D%20%40(%0A%22Test1%22%2C%0A%22test2%22%0A)%0A%0A%24test%20%7C%20export-csv%20C%3A%5CUsers%5CAdmin%5CDesktop%5CNameOfCSV.csv%20-delimiter%20'%3B'%0A%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EThats%20it.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20the%20official%20documentation%3A%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.utility%2Fexport-csv%3Fview%3Dpowershell-7.2%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.utility%2Fexport-csv%3Fview%3Dpowershell-7.2%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional 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 (Occasional 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 :)