#get files $files = get-childitem #set count to the first number that you added using the last script $i = 100 foreach ($file in $files) { #chop off the end $shortname = $file.name.trimend(".jpg") #chop off the set ID $shortname = $shortname.trimstart("CTDOP_") #make sure nothing is still there from the last iteration $splitarray = $null #split the string at the underscores (since I used them instead of spaces in the image name $splitarray = $shortname.split("_") #only run this if an underscore existed (ie, two words or more) if($splitarray.length -gt 1) { foreach ($split in $splitarray) { $newname += "$($split) " } } else { $newname = $splitarray } #add results to a CSV file. first column is ID, second is the image associated with that ID add-content C:\ecommerce\sql2.csv "$($newname),$($i)" $i++ $newname = $null }