Powershell with FileSystem Copy File and exclude files some types files

Using Powershell you can copy file fron one location to another location. Here we will see three scenario's

  1. 1. Copy file from one location to another.
  2. 2. Copy one type of file to another location
  3. 3. Copy all folder content (including subfolders) to another folder.

    Example

     
            # Here I define the folders and the extensions to look at.
            $folder1 = ls "C:\powershell-scripts\from\" -recurse -include "*.pdf"
            $destinationfolder = "C:\powershell-scripts\to\"
            Copy-Item -Path $folder1 -Destination $destinationfolder
        

    Copy and Try it

  4. if destination "To" folder is not present
  5. 4. Copy file from one location to another if destination "To" folder is not present
  6. 5. Copy one type of file to another location if to folder is not present
  7. 6. Copy all folder content (including subfolders) to another folder if to folder is not present.

    Example

     
            # Here I define the folders and the extensions to look at.
            $folder1 = ls "C:\powershell-scripts\from\" -recurse -include "*.pdf"
            $destinationfolder = "C:\powershell-scripts\to\"
            Copy-Item -Path $folder1 -Destination $destinationfolder
        

    Copy and Try it

  8. Move files or remove files from existing files once copy is done.
  9. 7. Copy file from one location to another if to folder is not present
  10. 8. Copy one type of file to another location if to folder is not present
  11. 9. Copy all folder content (including subfolders) to another folder if to folder is not present.
  12. File latest or recent generated file and copy it another location This is scenario is typically required in TFS migration or continuous integration or automated deployment.
  13. 10. Copy recent file from one location to another.
  14. 1. Copy file from one location to to remote server.
  15. 2. Copy one type of file to remote server
  16. 3. Copy all folder content (including subfolders) to shared path server.

    Example

     
            # Here I define the folders and the extensions to look at.
            $folder1 = ls "C:\powershell-scripts" -recurse -include "*.pdf"
            $destinationfolder = "\\DNYANU-PC\SharedPath"
            Copy-Item -Path $folder1 -Destination $destinationfolder
             

    Copy and Try it

  17. Copy files from source directory to target directory and exclude specific file types from specified directories

    Example

     
                    $SourceDirectory =   'C:\powershell-scripts\from'
                    $DestinationDirectory = 'C:\powershell-scripts\to'
                    $files = Get-ChildItem $SourceDirectory -Recurse -Exclude *.pdf
                    foreach ($file in $files)
                    {
                        write-host $file.FullName
        
                        Copy-Item $file.FullName -Destination $DestinationDirectory 
        
                    }
                    

    Copy and Try it



    Run the script

    output