Need Help with PowerShell Script

Greeting Experts,

I need help creating PowerShell script to read the name multiple files (over 2,000 files names) in a file directory and then import that information into a ".csv" document. Can somebody help me with this task...
MikeSecurityAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroCEOCommented:
Maybe if you could put an example of the dummy text information it could be more helpful for the end result.

$FolderPath="C:\file\path\ThisFolder"

$FolderPath = "D:\Cloud\OneDrive\Desktop Diciembre 2017\ee\text\files"
$outputfileName= "Output.csv"

$files=[System.IO.Directory]::GetFiles($FolderPath)

$AllFilesInfo=@()
if($files.Count -gt 0){
    foreach($file in $files){
        $content = Get-Content $file
        $AllFilesInfo+= $content
    }
    $AllFilesInfo | Export-Csv -NoTypeInformation -Path "$FolderPath\\$outputfileName"
}
else{
    Write-Warning "There are no files in that Folderpath:""$FolderPath"""
}

Open in new window

0
David Johnson, CD, MVPOwnerCommented:
function Get-Files
{
  <#
    .SYNOPSIS
    Gets list of filenames from a source directory and all subdirectories and exports to a csv file
   .EXAMPLE
    Get-Files -FolderPath c:\temp -OutputFileName output.csv
    
  #>
  [CmdletBinding()]
  param
  (
    [Parameter(Mandatory=$false, Position=0)]
    [System.String] $FolderPath = "C:\file\path\ThisFolder",
  [Parameter(Mandatory=$true, Position=1)]
     [String]$outputfileName= "Output.csv"
    )
  
 $AllFilesInfo = @()
  $files=Get-ChildItem -Path $FolderPath -Recurse
  
  if($files.Count -gt 0){
    foreach($file in $files){
 $FileInfo=New-Object -TypeName PSObject
 Add-Member -InputObject $FileInfo -MemberType NoteProperty "Filename" -Value $file.fullname

 $AllFilesInfo += $FileInfo
   }
    $AllFilesInfo | Export-Csv -NoTypeInformation -Path "$FolderPath\\$outputfileName"
  }
  else{
    Write-Warning "There are no files in that Folderpath:""$FolderPath"""
  }
}

Open in new window

0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Do you need to go thru subfolders too? Do you only need file names or more info?
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

MikeSecurityAuthor Commented:
I will be scanning a large file directory with subfolders ( 10,500 files) that go for about 10 layers.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
And what should your "CSV" look like? Note that a CSV is only a CSV if it has more than one column, otherwise it is just a text file ;-).
Whatsoever, the simplest approach is
Get-ChildItem C:\Temp\EE\files -recurse -Name | Out-File C:\Temp\EE\result.txt

Open in new window

1
oBdACommented:
There is no need for intermediate variables, especially when it comes to large file structures. It's far less resource intensive to let the pipeline do the work.
In line 2, select the properties you want exported, in the order in which you want them to appear as columns in the csv.
Start with a smaller folder until you're sure you have identified all the properties before going for the big one.
To see all the available properties, just enter
Get-Item S:\ome\file.ext | Select *
If you're only interested in certain file types, change the Filter argument accordingly; this is the fastest way, because items not matching won't reach the pipeline.
For more than one pattern, you can use the -Include (or -Exclude) argument (works only in conjunction with -Recurse!), as in
Get-ChildItem -Path C:\Source -Include *.doc?, *.xls?,
The files can be filtered in the pipeline as well for more complex patterns/properties (like age), but that'll be slower than using -Filter.
Get-ChildItem -Path C:\Source -Filter *.* -Recurse -File |
	Select-Object -Property Name, DirectoryName, LastWriteTime |
	Export-Csv -NoTypeInformation -Path C:\Temp\FileList.csv

Open in new window

Or as a one-liner:
Get-ChildItem -Path C:\Source -Filter *.* -Recurse -File | Select-Object -Property Name, DirectoryName, LastWriteTime | Export-Csv -NoTypeInformation -Path C:\Temp\FileList.csv

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MikeSecurityAuthor Commented:
It is going to be differnt types of files...docs, xlsx, pdf, and other ext .  I just want to get the name of the file and import it over to the .csv file
0
oBdACommented:
1) Do you really just the file name only, or the full path including the directory?
If you really want the file name only:
2a) Have you considered what you're going to do with duplicate file names, which is in the realm of the possible? You'll have no way to identify where the duplicate names were coming from.
2b) Do you really want a csv (with a header line of a single column name) or just a plain text file?
If you want the full file path including the directory:
2) Do you still want the files as one single path (one single column in a csv), or rather two separate columns (file name and directory name)?

3) Do you want all files under the root folder, or a filtered subset?
If a filtered subset only:
4a) Do you want a whitelist (return only file names matching a list of extensions) or a blacklist (return all files except for those matching a list of extensions)?
4b) What is the list of file extensions?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.