Need help on Powershell Copy-Item from network drives - powershell

I am trying to use Copy-Item from remote machine to another remote machine with the command:
Copy-Item -Path "\\machine1\abc\123\log 1.zip" -Destination "\\machine2\\c$\Logs\"
I am constantly getting Error "Cannot find Path "\\machine1\abc\123\log 1.zip"
I can access that path and copy manually from there.
I am opening PowerCLI as administrator and running this script... I am absolutely stuck here and not sure how to resolve it.

This seems to work as is on PowerShell v3. I don't have v2 handy to test with, but there are two options that I'm aware of, which ought to work. First, you could map PSDrives:
New-PSDrive -Name source -PSProvider FileSystem -Root \\machine1\abc\123 | Out-Null
New-PSDrive -Name target -PSProvider FileSystem -Root \\machine2\c$\Logs | Out-Null
Copy-Item -Path source:\log_1.zip -Destination target:
Remove-PSDrive source
Remove-PSDrive target
If this is something you're going to do a lot, you could even wrap this in a function:
Function Copy-ItemUNC($SourcePath, $TargetPath, $FileName)
{
New-PSDrive -Name source -PSProvider FileSystem -Root $SourcePath | Out-Null
New-PSDrive -Name target -PSProvider FileSystem -Root $TargetPath | Out-Null
Copy-Item -Path source:\$FileName -Destination target:
Remove-PSDrive source
Remove-PSDrive target
}
Alternately, you can explicitly specify the provider with each path:
Copy-Item -Path "Microsoft.PowerShell.Core\FileSystem::\\machine1\abc\123\log 1.zip" -Destination "Microsoft.PowerShell.Core\FileSystem::\\machine2\\c$\Logs\"

this works all day for me:
$strLFpath = "\\compname\e$\folder"
$strLFpath2 = "\\Remotecomputer\networkshare\remotefolder" #this is a second option that also will work
$StrRLPath = "E:\localfolder"
Copy-Item -Path "$StrRLPath\*" -Destination "$strLFpath" -Recurse -force -Verbose
things to watch:
Copy-item define the LAST item as the object.
for copying the content of a folder you NEED the \*
If you are copying the folder it self to a new location then you do not need to declare the content.

I use this daily:
Robocopy /E \\\SOURCEIP\C$\123\ \\\DESTIP\C$\Logs\
There is an empty space in the middle. For ROBCOPY, /E does a copy. You can google if you need to do a move.
Or:
$SourceIP = Read-Host "Enter the Source IP"
$DESTIP = Read-Host "Enter the Destination IP"
Robocopy /E \\\\$SourceIP\C$\123\ \\\\$DESTIP\C$\Logs\
####Just adjust the C$ path on both#####

Related

Powershell copy-item creates a folder instead of copying file

I mapped a webdav folder with new-psdrive, cd to a folder, then try to copy one file to a local folder. The script code (draft) is the following:
New-PSDrive -Name WEBDAV -PSProvider FileSystem -Root "\\testserver#SSL\davwwwroot\myApp"
cd WEBDAV:
cd "notes\user"
Copy-Item -Path ".\important.txt" -Destination "c:\temp"
What I see, that after the Copy-Item a new empty FOLDER "C:\temp\important.txt" is created. Not a file. When I change the "copy-item" command to "robocopy" it works perfectly. I am wondering what is wrong with the copy-item... How could I use it to do the work?!
simply specify the file name:
Copy-Item -Path ".\important.txt" -Destination "c:\temp.txt"

UNC Network Path issues with PowerShell

Omitted sensitive data.
I can't get PowerShell to connect to a shared folder.
Copy-Item : Cannot find path '\\192.168.0.000\Common Data\Software\Crystal Reports\CRRedist2005_X64.msi' because it does not exist.
This does work on one machine but not another.
$source = "\\192.168.0.000\Common Data\Software\Crystal Reports\CRRedist2005_X64.msi"
Copy-Item $source -Destination $env:USERPROFILE\Downloads -Verbose
This doesn't work on either machine
Copy-Item "\\192.168.0.000\Common Data\Software\Crystal Reports\CRRedist2005_X64.msi" -Destination $env:USERPROFILE\Downloads -Verbose
I've tried using the New-PSDrive method
New-PSDrive -Name S -Root "\\192.168.0.000\Common Data\" -PSProvider FileSystem
Copy-Item "S:\Software\Crystal Reports\CRRedist2005_X64.msi" -Destination $env:USERPROFILE\Downloads -Verbose
Gives The specified drive root "\\192.168.0.000\Common Data\" either does not exist, or it is not a folder.
The FileSystem:: also gives path not found
Copy-Item "FileSystem::\\192.168.0.000\Common Data\Software\Crystal Reports\CRRedist2005_X64.msi" -Destination $env:USERPROFILE\Downloads -verbose
If I copy the address into file explorer it works no problems.
I'm going to crazy trying to figure out why it's not working.
Edit: Remapped the drives and it worked on the second computer.

Empty folders when using Copy-Item Network Drive to Local Drive

I'm currently using this Powershell script
Function Copy-ItemUNC {
New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\ServerName\serverupdates\deploy\Program Files\"
Copy-Item -Path "\\servername\serverupdates\deploy\Program Files\*" -Destination 'C:/Program Files';
}
When I run the script it creates the folders but there are no subfolders within them.
Second problem I have is that I have to manually open Windows Explorer and type in the path to connect to it first in order for this script to even run. Is there a way to fix that too?
It's because you are not asking for subdirs, as that requires you to do this.
Copy-Item -Recurse
-Recurse <SwitchParameter>
Indicates that this cmdlet performs a recursive copy.
Required? false
Position? named
Default value False
Accept pipeline input? False
Accept wildcard characters? false
(Get-Command -Name Copy-Item).Parameters.Keys
Get-Help -Name Copy-Item -Full
Get-Help -Name Copy-Item -Examples
This command copies the mar1604.log.txt file to the C:\Presentation directory. The command does not delete the original file.
Example 2: Copy the contents of a directory to another directory
PS C:\>Copy-Item "C:\Logfiles" -Destination "C:\Drawings" -Recurse
So, this line should be like this..
Copy-Item -Path "\\servername\serverupdates\deploy\Program Files\*" -Destination 'C:/Program Files' -Recurse
You also do not really need this..
New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\ServerName\serverupdates\deploy\Program Files\"
... based on what you are after. Especially since you are not using it anywhere in your code. That semi-colon is also not needed.

How to use PowerShell and robocopy to backup files to a server?

I am just starting with PowerShell, so please be kind.
All I want to do is backup my directories and files from my laptop to the desktop computer, i.e. "server", using PowerShell and robocopy. I am the administrator to both machines (Windows 7).
This fails with access denied on the "server", i.e., desktop, despite the permissions being set for "Everybody" to do everything.
Any help (or better way) is really appreciated! Thanks.
$cred=get-credential
$sourcepath = ("\\localhost\C$\nova5");
$TargetPath = ("\\library\E$\nova5");
New-PSDrive -Name source -PSProvider FileSystem -Root $SourcePath
New-PSDrive -Name target -PSProvider FileSystem -Root $TargetPath -Credential $cred
robocopy source target /e;
return;
Psdrive is a feature for powershell cmdlet not for extrrnal command , change this line:
robocopy "\\localhost\C$\nova5" "$TargetPath" /e
You could try this:
$cred=get-credential
$sourcepath = C:\nova5 ;
$TargetPath = "\\library\E$\nova5"
New-PSDrive -Name target -PSProvider FileSystem -Root $TargetPath -Credential $cred
.\robocopy.exe $source $target "/e"

Add new item in context menu using powershell gives an error

I am trying to create a context menu but once its created i get an error message stating
This file does not have a program associated to it.
i am using this script . I am trying to create a powershell shortcut on folders.
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
Test-Path HKCR:\Directory\shell\Powershell
New-Item -Path HKCR:\Directory\shell -Name Powershell
Set-Item -Path HKCR:\Directory\shell\Powershell -Value "Open Powershell Here"
New-Item -Path HKCR:\Directory\shell\Powershell\key -Value
"C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -
LiteralPath '%L'"
Try to change last line:
New-Item -Path HKCR:\Directory\shell\Powershell\key -Value "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit -Command ""Set-Location -LiteralPath '%L'"""
If not work add a third '"' """Set-location...