Follow sqlserver-dba.com

Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions

SQLServer-DBA.com Links

Dba_db2_button

dba-ninja.com

SQL Server DBA Jobs

How to force a powershell copy-item file copy

05 November,2019 by Tom Collins

Question :  I'm attempting to use Powershell copy-item , to copy a file from a remote location onto a local drive. This is the code I'm executing :

 Copy-Item \\myremotelocation\servers.csv \\mylocaldrive\temp 

The problem I'm getting is a "denied" type of error:

Copy-Item : Access to the path '  \\mylocaldrive\temp\servers.csv   ' is denied.
At line:1 char:1
+ Copy-Item - \\myremotelocation\servers.csv...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (\\myremotelocation...servers.csv:FileInfo) [Copy-Item], Unauthorized
AccessException
+ FullyQualifiedErrorId : CopyFileInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand

I can copy the file manually i.e outside of the Powershell environment  and I am using the same logon for both the Powershell console and the manual drag and drop. 

How can I fix this issue?

Answer: Based on the error message -  you are missing using the -Force parameter . The error message can be misleading . The error message refers to  a path being denied - whereas the message in these circumstances means the item cannot be replaced. 

There is a full list of parameters if you use - help copy-file -full . 

Test this out by changing your code to :

Copy-Item -Force \\myremotelocation\servers.csv \\mylocaldrive\temp 

 

If you would like to know how to dig into Powershell read - Expand your Powershell mind – Three key cmdlets


Author: Tom Collins (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to force a powershell copy-item file copy


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer