{"id":7214,"date":"2022-11-15T06:25:00","date_gmt":"2022-11-15T05:25:00","guid":{"rendered":"https:\/\/expert-only.com\/uncategorized\/copy-a-file-with-windows-powershell\/"},"modified":"2022-11-14T15:46:03","modified_gmt":"2022-11-14T14:46:03","slug":"copy-files-with-powershell","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/ps\/copy-files-with-powershell\/","title":{"rendered":"Copy files with PowerShell and the Copy-Item command"},"content":{"rendered":"\n<h4 class=\"has-text-align-center wp-block-heading\"><strong><em>How to copy Windows files to another folder with PowerShell?<\/em><\/strong><\/h4>\n\n\n\n<p>There are several efficient options to copy Windows files with the PowerShell command prompt. In PowerShell scripting, use the Copy-Items command in a Windows PowerShell prompt. It is also possible to copy from the GUI, but this option is very manual. Indeed, it is always possible to copy files and folders using the interface provided by Windows. But it is more efficient to make large and specific copies.<\/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-with-powershell\/#copy-files-with-the-powershell-and-copy-item\" >Copy files with the PowerShell and Copy-Item<\/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-with-powershell\/#1-open-the-powershell-command-prompt\" >1. Open the PowerShell command prompt<\/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-with-powershell\/#2-copy-a-file-to-another-folder\" >2. Copy a file to another folder<\/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-with-powershell\/#3-script-to-copy-and-rename-a-file\" >3. Script to copy and rename a file<\/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-with-powershell\/#4-powershell-script-to-copy-all-files-with-a-filter\" >4. PowerShell script to copy all files with a filter<\/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-with-powershell\/#5-explicitly-enable-a-copy-confirmation-prompt\" >5. Explicitly enable a copy confirmation prompt<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-copy-files-with-the-powershell-and-copy-item\"><span class=\"ez-toc-section\" id=\"copy-files-with-the-powershell-and-copy-item\"><\/span>Copy files with the PowerShell and Copy-Item<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Indeed, Windows Shell scripting is much more efficient than manual copying. For example, to copy 500&#8217;000 text files with very specific extensions or names. And to make sure that they were all copied, without any error.<\/p>\n\n\n\n<p>Or with strict error checking and recording. Manually, this operation would take days. The PowerShell <em>Copy-Item<\/em> command is the best option available.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1-open-the-powershell-command-prompt\"><\/span>1. Open the PowerShell command prompt<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let&#8217;s start the tutorial about hoy copy a file with Windows PowerShell. From the Windows 10 start menu, for example, type &#8220;Windows PowerShell&#8221; in the search field and click on the icon.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2-copy-a-file-to-another-folder\"><\/span>2. Copy a file to another folder<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>PS command to copy a file from one folder to another existing folder keeping the same name and the same 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:\\folder\\file.txt -Destination C:\\newfolder\\file.txt<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3-script-to-copy-and-rename-a-file\"><\/span>3. Script to copy and rename a file<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>By the way, with Power Shell, to modify the name of the copy, simply change the name of the destination file.<\/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:\\folder\\file.txt -Destination C:\\folder\\newfile.txt<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4-powershell-script-to-copy-all-files-with-a-filter\"><\/span>4. PowerShell script to copy all files with a filter<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>That is, with a specific extension or a specific name before the separator. This sample script copies all text files, with a TXT extension, into the <em>newfolder<\/em> folder. And allows overwriting files if they already exist with the <strong><em>-Force<\/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:\\folder\\file*.txt -Destination C:\\newfolder -Force<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"981\" height=\"423\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/05\/powershell-copy-file-command-copy-Item-force_en.jpg\" alt=\"Copy files with Powershell from a Windows to another using the Copy-Item command\" class=\"wp-image-7312\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/05\/powershell-copy-file-command-copy-Item-force_en.jpg 981w, https:\/\/expert-only.com\/wp-content\/uploads\/2022\/05\/powershell-copy-file-command-copy-Item-force_en-300x129.jpg 300w, https:\/\/expert-only.com\/wp-content\/uploads\/2022\/05\/powershell-copy-file-command-copy-Item-force_en-768x331.jpg 768w\" sizes=\"auto, (max-width: 981px) 100vw, 981px\" \/><figcaption class=\"wp-element-caption\"><strong>Copy files with Powershell<\/strong><\/figcaption><\/figure>\n\n\n\n<p>Other Scripting options are available for the <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/samples\/working-with-files-and-folders?view=powershell-7.2\" target=\"_blank\" rel=\"noreferrer noopener\">Copy-Item<\/a> command, with for example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The first: <strong>-Force<\/strong> to overwrite the destination file.<\/li>\n\n\n\n<li>Or: <strong><strong>-Recurse<\/strong><\/strong> to copy recursively from source or destination, depending on where the keyword is in the command.<\/li>\n\n\n\n<li>The <strong>-Filter<\/strong> option to separate the filter from the copy source location.<\/li>\n<\/ul>\n\n\n\n<p>Note that this list of options is not exhaustive.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5-explicitly-enable-a-copy-confirmation-prompt\"><\/span>5. Explicitly enable a copy confirmation prompt<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Indeed, use these options to better manage and control file copies and avoid overwriting important, potentially unsaved files. This last copy PowerShell script example instructs the system to ask for confirmation before overwriting the files. Use the PowerShell confirmation option: <strong><em>-Confirm<\/em><\/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=\"\">Copy-Item -Path C:\\folder\\file.txt -Destination C:\\folder\\newfile.txt -Confirm<\/pre>\n\n\n\n<p>In conclusion, this first article in the Scripting with Windows PowerShell series shows Scripting PS examples for copying one or more files in Windows. To take it a step further, here &#8216;s <a href=\"https:\/\/expert-only.com\/en\/ms-dos\/copy-windows-files-with-cmd\/\">how to copy files and folders with a cmd script<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-embed 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=\"i0UqAjDUZ8\"><a href=\"https:\/\/expert-only.com\/fr\/ms-dos\/copier-des-fichiers-windows-en-cmd\/\">Comment copier des fichiers avec 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=\"\u00ab\u00a0Comment copier des fichiers avec cmd ?\u00a0\u00bb &#8212; Tutoriels SQL et IT\" src=\"https:\/\/expert-only.com\/fr\/ms-dos\/copier-des-fichiers-windows-en-cmd\/embed\/#?secret=VlAGPafjB3#?secret=i0UqAjDUZ8\" data-secret=\"i0UqAjDUZ8\" 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>How to copy Windows files to another folder with PowerShell? There are several efficient options to copy Windows files with the PowerShell command prompt. In PowerShell scripting, use the Copy-Items command in a Windows PowerShell prompt. It is also <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/ps\/copy-files-with-powershell\/\" title=\"Copy files with PowerShell and the Copy-Item command\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":5662,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[522],"tags":[412,417,418,414,524],"class_list":{"0":"post-7214","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-ps","8":"tag-batch","9":"tag-file","10":"tag-folder","11":"tag-script","12":"tag-scripting-en"},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/7214","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=7214"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/7214\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/5662"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=7214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=7214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=7214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}