Wednesday, 30 December 2015

Windows PowerShell: Recursive Function to List All Files in Folders and Sub-folders

Here is a simple recursive function to display all file-names with full path in folders and sub-folders. You can only run this command in Windows PowerShell available since the release of Windows 7.

$folder = "c:\\MyDirectory\\"

 Function Upload($item) {
    foreach ($i in Get-ChildItem $item)
    {
        Try
        {
            if((Get-Item $i.FullName) -is [System.IO.DirectoryInfo]){
                  Write-Output $i.FullName
                  Upload($i.FullName)

            }else{
              Write-Output $i.FullName
            }
        }catch{
            Write-Output $i.FullName
        }
    }   
}
 
Upload($folder)

Copy the above code and save in .ps1 file.

About the Author

Adnan Zameer, Lead Developer at Optimizley UK, is a certified Microsoft professional, specializing in web app architecture. His expertise includes Optimizley CMS and Azure, showcasing proficiency in crafting robust and efficient solutions.

0 comments :

Post a Comment