
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Copy Readonly and Hidden Files/Folders in PowerShell
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
Advertisements