Powershell Exclude folders and files from the current directory and subdirectories from copying

137 views Asked by At
   $exclude = @(
        "AppData",
        "Application Data",
        "Contacts",
        "Cookies",
        "IntelGraphicsProfiles",
        "Local Settings",
        "LtcJobs",
        "NetHood",
        "PrintHood",
        "Recent",
        "SendTo",
        "Searches",
        "главное меню",
        "Мои документы",
        "Мои видеозаписи",
        "мои рисунки",
        "Моя музыка",
        "Music",
        "MicrosoftEdgeBackups",
        "Saved Games",
        "Links",
        "Шаблоны",
        "All Users",
        "Default",
        "Default User",
        "Все пользователи",
        "LocAdmin",
        "*_wa",
        "*_adm",
        "*cache*",
        "*Cache*",
        "ntuser*",
        "Ntuser*",
        "NTUSER*",
        "*.tmp",
        "*.temp",
        "~*",
        "*.bak",
        "*.ini"
    );

    Copy-Item "${from}Users\" -Exclude $exclude -Recurse "$destinationFolderPath\Users\" -Force -PassThru *>&1 | Tee-Object -FilePath $pathToLogFile -Append;

There is the following code, is it possible to somehow correct it so that it excludes folders and files from the $exclude from copying, both in the current directory and in its subdirectories?

I tried adding * ("${from}Users\*"). But it doesn't work with subdirectories.

1

There are 1 answers

4
mohamed saeed On BEST ANSWER

use robocopy better

Notes:

if you want to delete the extra files in the destination use argument /mir and arg /e is to include subdirectories like recurse

  $excludeDir = @(
  "*Data",
  "Application Data",
  "Contacts"
)
$excludeFiles = @(
  "*.txt",
  "*.csv",
  "*.json"
)

robocopy C:\source C:\destination /XD  $excludeDir /XF $excludeFiles /e /mir