powershellsharepoint-onlinepnp.powershell

PnP - PowerShell - SharePoint - Getting all files in a library but omitting folder items


I'm using the PnP PowerShell module to interact with SharePoint lists/libraries. I'm running the following cmdlet to get a list of all the files in a library. This is working perfectly, except it returns all any folders in the library too. Is there anyway to omit folder items? I still want the files within the folders to be returned but just not the folders.

$files = (Get-PnPListItem -List $docLib.Title -Fields FileLeafRef","FileRef","Title").FieldValues

I suspect I can add a Where clause, however I'm not sure what property would identify an item as a folder. Any guidance would be appreciated. Thanks!

Many thanks

UPDATED: $files[0] | gm returns:

Add               Method                void Add(string key, System.Object value), void IDictionary[string,Object].Add(string key, System.Object value), void ICollection[KeyValuePair[stri...
Clear             Method                void Clear(), void ICollection[KeyValuePair[string,Object]].Clear(), void IDictionary.Clear()                                                         
Contains          Method                bool ICollection[KeyValuePair[string,Object]].Contains(System.Collections.Generic.KeyValuePair[string,System.Object] item), bool IDictionary.Contai...
ContainsKey       Method                bool ContainsKey(string key), bool IDictionary[string,Object].ContainsKey(string key), bool IReadOnlyDictionary[string,Object].ContainsKey(string key)
ContainsValue     Method                bool ContainsValue(System.Object value)                                                                                                               
CopyTo            Method                void ICollection[KeyValuePair[string,Object]].CopyTo(System.Collections.Generic.KeyValuePair[string,System.Object][] array, int arrayIndex), void I...
Equals            Method                bool Equals(System.Object obj)                                                                                                                        
GetEnumerator     Method                System.Collections.Generic.Dictionary`2+Enumerator[string,System.Object] GetEnumerator(), System.Collections.Generic.IEnumerator[System.Collections...
GetHashCode       Method                int GetHashCode()                                                                                                                                     
GetObjectData     Method                void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context), void ISerializable....
GetType           Method                type GetType()                                                                                                                                        
OnDeserialization Method                void OnDeserialization(System.Object sender), void IDeserializationCallback.OnDeserialization(System.Object sender)                                   
Remove            Method                bool Remove(string key), bool IDictionary[string,Object].Remove(string key), bool ICollection[KeyValuePair[string,Object]].Remove(System.Collection...
ToString          Method                string ToString()                                                                                                                                     
TryGetValue       Method                bool TryGetValue(string key, [ref] System.Object value), bool IDictionary[string,Object].TryGetValue(string key, [ref] System.Object value), bool I...
Item              ParameterizedProperty System.Object Item(string key) {get;set;}, System.Object IDictionary.Item(System.Object key) {get;set;}                                               
Comparer          Property              System.Collections.Generic.IEqualityComparer[string] Comparer {get;}                                                                                  
Count             Property              int Count {get;}                                                                                                                                      
IsFixedSize       Property              bool IsFixedSize {get;}                                                                                                                               
IsReadOnly        Property              bool IsReadOnly {get;}                                                                                                                                
IsSynchronized    Property              bool IsSynchronized {get;}                                                                                                                            
Keys              Property              System.Collections.Generic.Dictionary`2+KeyCollection[string,System.Object] Keys {get;}                                                               
SyncRoot          Property              System.Object SyncRoot {get;}                                                                                                                         
Values            Property              System.Collections.Generic.Dictionary`2+ValueCollection[string,System.Object] Values {get;}  

UPDATED: $files[0] returns the following (you can see it's returns a folder called 'General'):

FileLeafRef                General                                                                                                                                                            
FileRef                    /sites/Finance/Shared Documents/General                                                                                                                            
Title                      General                                                                                                                                                            
HTML_x0020_File_x0020_Type Team.Channel                                                                                                                                                       
MetaInfo                   vti_isexecutable:BW|false...                                                                                                                                       
_ModerationStatus          0                                                                                                                                                                  
_Level                     1                                                                                                                                                                  
Last_x0020_Modified        2022-04-03T19:52:10Z                                                                                                                                               
ID                         1                                                                                                                                                                  
UniqueId                   10d788d1-0308-4768-b47b-33f79723386e                                                                                                                               
owshiddenversion           2                                                                                                                                                                  
FSObjType                  1                                                                                                                                                                  
Created_x0020_Date         2022-01-26T10:35:55Z                                                                                                                                               
ProgId                     Team.Channel                                                                                                                                                       
Modified                   1/26/2022 10:35:56 AM                                                                                                                                              
CheckoutUser                                                                                                                                                                                  
ScopeId                    {3E95DFBF-DEF4-4FC8-AE50-C84D4E2F4483}                                                                                                                             
Editor                     Microsoft.SharePoint.Client.FieldUserValue        

UPDATED: Here are the properties of a file, the FSObjType is different, looks like 1 for folders, and 0 for files:

FileLeafRef                Document.docx                                                                                                                                                      
FileRef                    /sites/Finance/Shared Documents/General/Document.docx                                                                                                              
Title                      test                                                                                                                                                               
HTML_x0020_File_x0020_Type                                                                                                                                                                    
MetaInfo                   vti_mediaservicemetadata:SW|{"ctag":"\\"c:{7799CB41-34F1-40F5-A701-BFF6AAC846D0},5\\"","generationTime":"2022-01-26T12:13:38.1337171Z","buildVersion":"Media_PRO...
_ModerationStatus          0                                                                                                                                                                  
_Level                     1                                                                                                                                                                  
Last_x0020_Modified        2022-01-27T09:04:07Z                                                                                                                                               
ID                         2                                                                                                                                                                  
UniqueId                   7799cb41-34f1-40f5-a701-bff6aac846d0                                                                                                                               
owshiddenversion           6                                                                                                                                                                  
FSObjType                  0                                                                                                                                                                  
Created_x0020_Date         2022-01-26T12:12:31Z                                                                                                                                               
ProgId                                                                                                                                                                                        
Modified                   1/27/2022 9:04:06 AM                                                                                                                                               
CheckoutUser                                                                                                                                                                                  
ScopeId                    {3E95DFBF-DEF4-4FC8-AE50-C84D4E2F4483}                                                                                                                             
Editor                     Microsoft.SharePoint.Client.FieldUserValue     

So I've updated my call to the following which is now working perfectly, thanks Toni!

$files = (Get-PnPListItem -List $docLib.Title -Fields "FileLeafRef","Title").FieldValues | Where {$_.FSObjType -eq "0"} 

Solution

  • Please see updated description, FSObjType will allow you to pull back files or folders (0 for files, 1 for folders)