PowerShell File Copy Directory Check


        # -------------------------------------------------------------------------------------
        # Used DOS to copy over a full directory structure
        # Need a way to place some file in structure and trap for missing dir structure 
        # Will copy over files looking to match the structure on source and destination
        # Will toss error message when file is not written due to error to log file
        #-------------------------------------------------------------------------------------

        cls
        

        #----------------------------------
        #File Log for Good and Bad Results
        #----------------------------------

        $LogFile = "C:\Temp\MoveitLog-" + "_$(get-date -format "yyyyMMdd_hhmmsstt").txt"
        $LogFileError = "C:\Temp\MoveitLogError-" + "_$(get-date -format "yyyyMMdd_hhmmsstt").txt"
        

        #-----------------------------------------------------------------------
        #      Gets the list of all files from folder and all sub folers
        #----------------------------------------------------------------------- 

        $Files=Get-ChildItem -file C:\temp2 -Recurse | select fullname, directory 
        
        #Counter
        $x=1
        
 
      #-------------------------
        # Loops through All Files
        #-------------------------

        Foreach($File in $Files)
        {
        

        #------------------------------------------------------------------
        # Returns the path / directory of the file - drops the file name
        #------------------------------------------------------------------

        $New_String = $File.Directory.ToString()
        

        #-----------------------------------------------------------
        # Drops the start of the directoy path - not needed for UNC 
        #-----------------------------------------------------------

        $New_String = $New_String.Trim("C:\")
        
        
        Try {

                #--------------------------------------------------------------------------
                #Actually attempts to move item from current location to remote location  
                #--------------------------------------------------------------------------

                move-item $File.fullname "\\YourServer\Test_Share\$New_String\"  -ErrorAction Stop
                
                "$x. $Dates Success- File Name: " + $File.fullname.ToString() + " move to location: \\YourServer\Test_Share\$New_String\" | Out-File $LogFile -Append
        
                Write-host "$x. $Dates Success- File Name: " + $File.fullname.ToString() + " move to location: \\YourServer\Test_Share\$New_String\"
            }
        Catch
            {
                #For Date time Stamping 
                $Dates=Get-Date
                #Error message of file and remote location of where its trying to place it.
                "$x. Error $Dates - File Name: " + $File.fullname + " move to location: \\YourServer\Test_Share\$New_String\ - Error Message: $_ " | Out-File $LogFileError -Append            
        
                write-host "Error $Dates - File Name: " + $File.fullname + " move to location: \\YourServer\Test_Share\$New_String\ - Error Message: $_ "
             
        }        
        #Counter Incremental
        $x=$x+1
        }                
        

2020 | Scott W. Head  |  Sr. Systems Admin | Certified Microsoft Windows Server Administrator