To export the directory listing to csv file using PowerShell, you can use the Get-ChildItem cmdlet with the -Directory switch to list the directory and the Export-CSV cmdlet to export the directory listing to a csv file.
You can use the following method to export the folder list to CSV.
Method 1: Export PowerShell directory listing to CSV
# specify the directory path
$directoryPath = "C:\temp\log\"
# Export folder list to csv
Get-ChildItem -Path $directoryPath -Directory | Select-Object Name, CreationTime | Export-Csv -Path "C:\temp\directory_listing.csv"
This example will export the directory list to the CSV file.
The following example shows how to use this method with syntax.
Export Directory Listing to CSV Using PowerShell
Use the Get-ChildItem cmdlet with the -Directory parameter to list directories only and the Export-CSV cmdlet to export them to CSV file.
# specify the directory path $directoryPath = "C:\temp\log\" # Export folder list to csv Get-ChildItem -Path $directoryPath -Directory | Select-Object Name, CreationTime | Export-Csv -Path "C:\temp\directory_listing.csv" # Read the content of directory_listing.csv file Get-Content -Path "C:\temp\directory_listing.csv"
Output:
In the above PowerShell script, the $directoryPath variable holds the directory path.
The Get-ChildItem command retrieves the folders only using the -Directory parameter and pipes them to the Select-Object cmdlet to select the Name and CreationTime for the directories and pass it to the Export-CSV cmdlet for exporting directory listing to CSV file.
Conclusion
I hope the above article on listing directory to csv file using PowerShell is helpful to you.
You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.