Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Powershell - Copy remote files to a network drive with credentials
$secpassword = ConvertTo-SecureString "********" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ("**********", $secpassword)
$targetip = "*************"
$localfolder = "C:\path\to\local\folder\"
$networkfolder = "\\networkfileserver\path\to\network\folder\"
$remotefolder = "/opt/path/to/remote/folder/"
#Remove old files from local directory
if(!(Test-Path -Path $localfolder )){
New-Item -ItemType directory -Path $localfolder
} else {
Remove-Item -path $localfolder -recurse -force
New-Item -ItemType directory -Path $localfolder
}
#Download files
Get-SCPFolder -ComputerName $targetip -Credential $creds -Localfolder $localfolder -RemoteFolder $remotefolder
#Move files to network folder
Foreach($file in Get-ChildItem $localfolder)
{
$path = $localfolder + "\" + $file.name
Move-Item -Path $path -Destination $networkfolder -Force
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment