In this PowerShell tutorial, we will see how to read all files from folders and subfolders in PowerShell.
These days all the clients are looking for migrating their data from the local environment to SharePoint/Office 365 for better collaboration.
As part of this migration process for better progress and quick migration, we need to implement some automation process. The best way(s) to migrate data from local folders/drives to SharePoint or Office 365 is:
- PowerShell scripting
- Implementing windows application with the managed client-side object model
- ECMA script to upload files
As discussed above I would like to discuss the first approach PowerShell scripting. To read all the files from the local drive/folder we need to make use of PowerShell recursive function.
PowerShell read all files from folders and subfolders
Here I have a folder in my local drive and it can have subfolders or nested subfolders. By using PowerShell recursive function, we will read the file names inside the folder and subfolders.
try {
function getAllFile([string]$path) {
$fc = new-object -com scripting.filesystemobject
$folder = $fc.getfolder($path)
Write-Host "Folder Name="$folder.Name "Path="$path
foreach ($i in $folder.files) {
Write-Host "`nFile Name::" $i.Name
Write-Host "File Path::" $i.Path
}
foreach ($i in $folder.subfolders) {
getAllFile($i.path)
}
}
getAllFile "G:\KVN Articles"
}
catch {
Write-Host "`n Error:: $($_.Exception.Message)" -ForegroundColor Red -BackgroundColor Yellow
}
Let us walk through the code:
- new-object -com scripting.filesystemobject – To get the object of the file system
- $fc.getfolder($path) – To get object of folder
- $folder.subfolders – To get object of folder
The output will be as follows:
You may like the following PowerShell tutorials:
- Get sharepoint site template PowerShell
- Display warning message in SharePoint site using PowerShell
- How to get file modification time and date using PowerShell
- How to remove SharePoint Online Modern Page Title using PowerShell
- The file is not digitally signed PowerShell script
- How to activate SharePoint Publishing Feature Programmatically using CSOM and PowerShell
- Get SharePoint List Name and GUID using PowerShell and CSOM
Here, we learned how to read all files from folders and subfolders using PowerShell.
I am Krishna.Vandanapu a SharePoint architect working in IT from last 13+ years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019