{"id":9685,"date":"2023-07-05T05:38:00","date_gmt":"2023-07-05T03:38:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=9685"},"modified":"2023-09-12T12:29:13","modified_gmt":"2023-09-12T10:29:13","slug":"copy-files-recursively-in-powershell","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/ps\/copy-files-recursively-in-powershell\/","title":{"rendered":"How to copy files recursively in PowerShell using Copy-Item?"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\" id=\"h-this-microsoft-tutorial-explains-how-to-copy-files-and-folders-recursively-in-powershell-using-the-windows-copy-item-command\"><strong><em>This Microsoft tutorial explains how to copy files and folders recursively in PowerShell, using the Windows Copy-Item command.<\/em><\/strong><\/h4>\n\n\n\n<p>The Copy-Item command allows you to copy Windows files recursively in PowerShell, using a single command. The user can run scripts on Windows servers or clients, which are similar to the batch files of older versions of Windows, i.e. MS-DOS commands. Note that both <a href=\"https:\/\/expert-only.com\/en\/ms-dos\/\">DOS commands<\/a> and PowerShell are available on Windows 10 and Windows 11 for example as well as in previous Windows versions.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/expert-only.com\/en\/ps\/copy-files-recursively-in-powershell\/#1-powershell-scripts-and-the-copy-item-command\" >1. PowerShell scripts and the Copy-Item command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/expert-only.com\/en\/ps\/copy-files-recursively-in-powershell\/#2-list-recursively-files-folders-and-subfolders-before-the-copy\" >2. List recursively files, folders, and subfolders before the copy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/expert-only.com\/en\/ps\/copy-files-recursively-in-powershell\/#3-copy-files-recursively-from-source-folder-to-target-with-absolute-paths\" >3. Copy files recursively from source folder to target with absolute paths<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/expert-only.com\/en\/ps\/copy-files-recursively-in-powershell\/#4-check-the-target-folder-after-the-copy\" >4. Check the target folder after the copy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/expert-only.com\/en\/ps\/copy-files-recursively-in-powershell\/#5-copy-files-recursively-and-force-overwrite-of-the-target\" >5. Copy files recursively and force overwrite of the target<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/expert-only.com\/en\/ps\/copy-files-recursively-in-powershell\/#6-selecting-specific-file-type-extensions-to-copy\" >6. Selecting specific file type extensions to copy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/expert-only.com\/en\/ps\/copy-files-recursively-in-powershell\/#7-exclude-files-from-the-powershell-copy\" >7. Exclude files from the PowerShell copy<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-1-powershell-scripts-and-the-copy-item-command\"><span class=\"ez-toc-section\" id=\"1-powershell-scripts-and-the-copy-item-command\"><\/span>1. PowerShell scripts and the Copy-Item command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>First of all, <strong>Windows PowerShell is a task-based command-line shell and scripting language that is built on the .NET Framework<\/strong>. Windows PowerShell includes a cmdlet (command-line tool) for copying files. Cmdlets are like small commands that can be combined to perform tasks such as copying files. <\/p>\n\n\n\n<p>The script language will help you <strong>reduce the time it takes to copy files, automate tasks, manage system configurations<\/strong>, and more. The Copy-Item cmdlet can be used to copy one or more items from one location to another, such as from a file system folder to a CD drive, or from one folder in a file system tree to another folder in the same tree.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2-list-recursively-files-folders-and-subfolders-before-the-copy\"><\/span>2. List recursively files, folders, and subfolders before the copy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The script starts by defining the location of the source folder and then copies all <a href=\"https:\/\/www.dictionary.com\/browse\/file\" target=\"_blank\" rel=\"noreferrer noopener\">files<\/a> and folders from that location to the destination folder. For example, here is a simple, practical case, a <strong>C:\\WORK\\Folder<\/strong> source folder with two subfolders:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>Folder<\/em><\/strong> contains<ul><li>file (1).txt<\/li><\/ul>\n<ul class=\"wp-block-list\">\n<li>file (2).txt<\/li>\n\n\n\n<li>file (3).txt<\/li>\n\n\n\n<li>The <strong><em>SubFolder_1<\/em><\/strong> folder contains\n<ul class=\"wp-block-list\">\n<li>file (4).txt<\/li>\n\n\n\n<li>file (5).txt<\/li>\n\n\n\n<li>file (6).txt<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>The goal is to copy all the files and the tree structure into the target folder: <strong>C:\\WORK\\Target<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Get-ChildItem C:\\WORK\\Folder -Recurse<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"669\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/08\/powershell-display-source-folder-files-recursively.jpg\" alt=\"List all files recursively with the PowerShell Get-ChildItem command\" class=\"wp-image-9647\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/08\/powershell-display-source-folder-files-recursively.jpg 768w, https:\/\/expert-only.com\/wp-content\/uploads\/2022\/08\/powershell-display-source-folder-files-recursively-300x261.jpg 300w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><figcaption class=\"wp-element-caption\">List all files recursively with the PowerShell Get-ChildItem command<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3-copy-files-recursively-from-source-folder-to-target-with-absolute-paths\"><\/span>3. Copy files recursively from source folder to target with absolute paths<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this third section, we present this time a script to copy all files in folders and subfolders with the Copy-Item command and the <strong><em>-Recurse<\/em><\/strong> option.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Copy-Item -Path C:\\WORK\\Folder\\ -Destination C:\\WORK\\Target -Recurse<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"797\" height=\"186\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/08\/powershell-copy-item-files-recursively.jpg\" alt=\"Copy files recursively in PowerShell with Copy-Item\" class=\"wp-image-9654\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/08\/powershell-copy-item-files-recursively.jpg 797w, https:\/\/expert-only.com\/wp-content\/uploads\/2022\/08\/powershell-copy-item-files-recursively-300x70.jpg 300w, https:\/\/expert-only.com\/wp-content\/uploads\/2022\/08\/powershell-copy-item-files-recursively-768x179.jpg 768w\" sizes=\"auto, (max-width: 797px) 100vw, 797px\" \/><figcaption class=\"wp-element-caption\">Copy files recursively in PowerShell with Copy-Item<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4-check-the-target-folder-after-the-copy\"><\/span>4. Check the target folder after the copy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>It is also possible to check that the files are copied to the target folder:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Get-ChildItem C:\\WORK\\Target -Recurse<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"720\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/08\/powershell-display-target-folder-files-recursively.jpg\" alt=\"Display files and folders in the target folder recursively in PowerShell\" class=\"wp-image-9660\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/08\/powershell-display-target-folder-files-recursively.jpg 720w, https:\/\/expert-only.com\/wp-content\/uploads\/2022\/08\/powershell-display-target-folder-files-recursively-300x300.jpg 300w, https:\/\/expert-only.com\/wp-content\/uploads\/2022\/08\/powershell-display-target-folder-files-recursively-150x150.jpg 150w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><figcaption class=\"wp-element-caption\">Display files and folders in the target folder recursively in PowerShell<\/figcaption><\/figure>\n<\/div>\n\n\n<p>The script has successfully copied all files and folders from the WORK folder, <strong>including the folder named Folder<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5-copy-files-recursively-and-force-overwrite-of-the-target\"><\/span>5. Copy files recursively and force overwrite of the target<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To force the copy if one or more folders or files already exist in the target folder, use the -Force option.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Copy-Item -Path C:\\WORK\\Folder\\ -Destination C:\\WORK\\Target -Recurse -Force<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"798\" height=\"186\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/08\/powershell-copy-item-files-recursively-force-option.jpg\" alt=\"Copy files recursively in PowerShell with Copy-Item and overwrite the target\" class=\"wp-image-9670\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/08\/powershell-copy-item-files-recursively-force-option.jpg 798w, https:\/\/expert-only.com\/wp-content\/uploads\/2022\/08\/powershell-copy-item-files-recursively-force-option-300x70.jpg 300w, https:\/\/expert-only.com\/wp-content\/uploads\/2022\/08\/powershell-copy-item-files-recursively-force-option-768x179.jpg 768w\" sizes=\"auto, (max-width: 798px) 100vw, 798px\" \/><figcaption class=\"wp-element-caption\">Copy files recursively in PowerShell with Copy-Item and overwrite the target<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6-selecting-specific-file-type-extensions-to-copy\"><\/span>6. Selecting specific file type extensions to copy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The script also provides an option to copy only specific file types, such as *.docx or *.jpg, or a specific file with a given name. For example, to force the copy of TXT files only and ignore all other extensions, use this script:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Copy-Item -Path C:\\WORK\\Folder\\ -Destination C:\\WORK\\Target -Recurse -Force -Filter \"*.txt\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"7-exclude-files-from-the-powershell-copy\"><\/span>7. Exclude files from the PowerShell copy <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Finally, the script offers an option to exclude certain directories from the copy. To exclude file extensions, use the PS -Exclude option. In this example, the copy is recursive and copies all files except Word files with a Docx extension.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Copy-Item -Path C:\\WORK\\Folder\\ -Destination C:\\WORK\\Target -Recurse -Exclude \"*.docx\"<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-conclusion-on-recursive-file-copy-using-powershell\">Conclusion on recursive file copy using PowerShell<\/h3>\n\n\n\n<p>To conclude, this article details how to use PowerShell&#8217;s Copy-Item command for file management. It teaches readers to list files recursively using the Get-ChildItem command, copy files with the -Recurse option, verify copied content in the target location, and overwrite existing files with the -Force option. Still with Windows commands, it is also possible to perform the same <a href=\"https:\/\/expert-only.com\/en\/ms-dos\/copy-recursively-files-and-folders-in-cmd-with-xcopy\/\">recursive copy of files and folders with the DOS xcopy command<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"gHgcojFGhX\"><a href=\"https:\/\/expert-only.com\/en\/ms-dos\/copy-files-recursively-with-cmd\/\">How to copy files recursively using Windows cmd?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;How to copy files recursively using Windows cmd?&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/ms-dos\/copy-files-recursively-with-cmd\/embed\/#?secret=EDPIvx2F6n#?secret=gHgcojFGhX\" data-secret=\"gHgcojFGhX\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>This Microsoft tutorial explains how to copy files and folders recursively in PowerShell, using the Windows Copy-Item command. The Copy-Item command allows you to copy Windows files recursively in PowerShell, using a single command. The user can run scripts <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/ps\/copy-files-recursively-in-powershell\/\" title=\"How to copy files recursively in PowerShell using Copy-Item?\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":6058,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[522],"tags":[],"class_list":{"0":"post-9685","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-ps"},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/9685","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=9685"}],"version-history":[{"count":3,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/9685\/revisions"}],"predecessor-version":[{"id":26942,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/9685\/revisions\/26942"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/6058"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=9685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=9685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=9685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}