Powershell to get user drives file details separatly for each user and invaid

Write-Host ‘This Powershell Script will Get the User Drives File details separalty for each user and invalid file names and export to CSV file’

try
{

   #Get Users Folder path
   $UsersSharedPath = "S:\Users\*"; 

   #Set OutPut file path       
   $OutPutReportPath = $myinvocation.mycommand.path.Replace($MyInvocation.MyCommand.name,"");
  
   #Get Users from mapped folder
   $UsersCollection = Get-ChildItem $UsersSharedPath -Directory 

   #Get an array of invalid characters 
   $arrInvalidChars = [System.IO.Path]::GetInvalidFileNameChars() 

   $invalidCharsNoSpace = [RegEx]::Escape(-join $arrInvalidChars) 

   #Loop the collection of imported user names and their drive location
   #Drive path should be like \\servername\E:\
   for($i=1; $i -le 10; $i++)
   {

      #Declare the array variable
      $row= @()

      $invalidFiles = @()
      
      #get user name and drive path from input file  
      $userName = $UsersCollection[$i].Name 

      #Drive path will be user name as each folder will have User name as its main folder name
      $DrivePath = $UsersSharedPath + $userName

      #Get Files info for the user
      $files = Get-ChildItem $DrivePath -Recurse -File

      for($j=0; $j-le $files.Count; $j++)
      {
         #Generate report only if the file size is greater than 2 GB
         $fileSize = [math]::round($files[$j].Length /1Gb, 3)

         if($fileSize -gt 2)
         {
                  #Create a new instance of collection and add above retrieved values
                  $row += New-Object -TypeName PSObject -Property @{
                                                            EmployeeId =  $userName
                                                            FileFullName = $files[$j].Name
                                                            FileLocation =  $files[$j].FullName
															FileSizeInGB = $fileSize
                                                            } | Select-Object EmployeeId,FileFullName,FileLocation,FileSizeInGB
          }

          if($files[$j].Name -match "[$invalidCharsNoSpace]")
          {
             #Create a new instance of collection and add above retrieved values
                  $invalidFiles += New-Object -TypeName PSObject -Property @{
                                                            EmployeeId =  $userName
                                                            FileFullName = $files[$j].Name
                                                            FileLocation =  $files[$j].FullName
                                                            } | Select-Object EmployeeId,FileFullName,FileLocation
              
          }
      }

       
       try
       {
          if($row -ne $null)
          {    
            $csvFileName = $OutPutReportPath +  $userName + "-FileInfo.csv"
            $row | Export-Csv  $csvFileName -NoTypeInformation -Force  -ErrorAction SilentlyContinue
          }
 
          else
          {
            Write-Host "No Records Found"
          }

          
          if($invalidFiles -ne $null)
          {    
            $csvInvalidCharFileName = $OutPutReportPath +  $userName + "-InvalidCharFileInfo.csv"
            $invalidFiles | Export-Csv $csvInvalidCharFileName -NoTypeInformation -Force  -ErrorAction SilentlyContinue
          }
 
          else
          {
            Write-Host "No Records Found"
          }
       }
        
      catch
      {
         Write-Host "Error Details. " $_
      }
     
    }
 }
catch
{
  Write-Host "Error Details. " $_
}

write-host “Script Ended at $(get-date)”

Gives an output
Error details. You cannot call a method on a null-valued expression.

Please help me on this

It would be really useful if you gave the entire error message.

Hi Bob,

The error message is
Error details. You cannot call a method on a null valued expression.
Script ended at it giving data and time.

This is the out put after I run the above script

Does it not give a line number?

Hi Reddy,

Without the line number as Bob said it wastes a lot of time. Also put your code inside code blocks.

I noticed an issues at the start of your script.

$UsersSharedPath = "S:\Users\*"; 
$DrivePath = $UsersSharedPath + $userName

# Note $DrivePath has an asterisk in it for example S:\Users\*HSIMPSON

$files = Get-ChildItem $DrivePath -Recurse -File

Get-ChildItem will run but with the asterisk in there it won’t give you back the file names you want.

Michael