ABCD
ABCD

Reputation: 19

Powershell get extension files within a given path

The main idea of this post is that, given a path to folder or disk. Then powershell will search all file and folder inside this path and list all the extension file current stored in this path (docx, xlsx, jpg,...)

The final results will be stored in csv file.

Upvotes: 0

Views: 1562

Answers (3)

Sergio Tanaka
Sergio Tanaka

Reputation: 1435

I've made a little fix in the svanzundert solution

$Folder = "C:\Temp\"
Get-ChildItem $Folder | Select Extension -Unique | Where-Object{$_.Extension -ne ""} | Export-Csv -Path C:\Temp\YourCSV.csv -NoTypeInformation

You just have to use -Unique parameter in the Select-Object

Upvotes: 1

xxxvodnikxxx
xxxvodnikxxx

Reputation: 1277

For sure there will be better ways, like use filter to get from childs files onlz but zou can do something like follows:

#will load directory content
$files = Get-ChildItem "C:\Users\fooo\Documents\" 

#empty array for extensions preparation
$extensions = New-Object System.Collections.ArrayList($null)

#iterate over items= files and directories
for ($i=0; $i -lt $files.Count; $i++) {
    #print all (once by one of course)
    #Write-Host $files[$i].FullName
    #print extension only
    #Write-Host $files[$i].Extension

    #store extensions by unique, if its a file
    if( ($files[$i].GetType().Name -eq "FileInfo" ) -and !$extensions.Contains($files[$i].Extension) ){
        $extensions.Add($files[$i].Extension)
    }
}

Write-Host $extensions

my output:

.gitattributes .gitignore .mailmap .yml .cmd .fsx .sh .md .dependencies .lock .sln .DotSettings

Upvotes: 0

svanzundert
svanzundert

Reputation: 63

I think you want something like this, but could be I misunderstood your question.

$Folder = "C:\Temp\"
Get-ChildItem $Folder | Select Extension | Where-Object{$_.Extension -ne ""} | Export-Csv -Path C:\Temp\YourCSV.csv -NoTypeInformation

Upvotes: 1

Related Questions