How to copy readonly and hidden files/folders in the PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To copy the readonly and hidden items from one location to another, you need to use the –Force parameter with Copy-Item cmdlet.

When you run the command without Force parameter for the readonly/hidden files, you will get the error. An example is given below.

Example

Copy-Item D:\Temp\Readonlyfile.txt -Destination D:\TempContent\

Output

PS C:\WINDOWS\system32> Copy-Item D:\Temp\Readonlyfile.txt -Destination D:\TempContent\
Copy-Item : Access to the path 'D:\TempContent\Readonlyfile.txt' is denied.
At line:1 char:1
+ Copy-Item D:\Temp\Readonlyfile.txt -Destination D:\TempContent\
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   + CategoryInfo : PermissionDenied: (D:\Temp\Readonlyfile.txt:FileInfo) [Copy-Item], UnauthorizedAccessException
   + FullyQualifiedErrorId : CopyFileInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand

When you add the –Force parameter in the cmdlet, it can copy the readonly/ hidden files as well.

Example

An example is given below for the readonly file.

Copy-Item D:\Temp\Readonlyfile.txt -Destination D:\TempContent\ -Force -PassThru

Output

PS C:\WINDOWS\system32> Copy-Item D:\Temp\Readonlyfile.txt -Destination D:\TempContent\ -Force -PassThru
    Directory: D:\TempContent
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-ar---       13-01-2020     18:19              0 Readonlyfile.txt

Example

The below example is for the Hidden file.

Copy-Item D:\Temp\hiddenfile.xlsx -Destination D:\TempContent\ -Force -PassThru

Output

PS C:\WINDOWS\system32> Copy-Item D:\Temp\hiddenfile.xlsx -Destination D:\TempContent\ -Force -PassThru
    Directory: D:\TempContent
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a-h--       13-12-2019     09:52           6182 hiddenfile.xlsx
raja
Published on 12-Mar-2020 07:04:09
Advertisements