I had a task to do today that required me to get all the names in a directory of files into a database. This seemed like a ideal job for Powershell and I’ve posted the (very simple) script here.

There’s a few variables you need to change to fit your environment; $directory - this should contain the path to the directory of files you need the filenames for. $txtFile – this is the text file where the names will be written to. It does not have to exist first.

# The directory containing your files
$directory = "C:\Users\Rhys\Desktop\country_gif";
# The text file to write filenames to (does not have to exist first)
$txtFile = "C:\Users\Rhys\Desktop\files.txt"

# Get filenames from this directory
# Get-ChildItem is aliased by 'dir'
$files = Get-ChildItem $directory;

# Loop through the files in the directory
foreach($file in $files)
{
	Add-Content $txtFile $file.Name;
}