Prompt Title: Backup files to remote server.

Created 1 year ago
Votes 0
Views 334 times
0

Prompt Detail:

To backup files to a shared folder on a remote server with PowerShell, you can use the following steps:

  1. Connect to the remote server using the New-PSDrive cmdlet. This creates a persistent mapping to the remote folder that you can reference in your commands.
powershell
New-PSDrive -Name "Remote" -PSProvider "FileSystem" -Root "\\RemoteServer\SharedFolder"
  1. Change the location to the remote folder using the Set-Location cmdlet. This is equivalent to navigating to the folder in Windows Explorer.
powershell
Set-Location "Remote:"
  1. Use the Copy-Item cmdlet to copy the files from your local machine to the remote folder. You can use wildcards to specify a pattern of files to copy.
powershell
Copy-Item "C:\Files\*.txt" -Destination "Remote:\Backup"
  1. Disconnect from the remote server using the Remove-PSDrive cmdlet. This closes the connection to the remote folder.
powershell
Remove-PSDrive "Remote"

Putting it all together, here's an example script to backup all .docx files from C:\Documents on your local machine to \\RemoteServer\SharedFolder\Backup:

powershell
New-PSDrive -Name "Remote" -PSProvider "FileSystem" -Root "\\RemoteServer\SharedFolder" Set-Location "Remote:" Copy-Item "C:\Documents\*.docx" -Destination "Remote:\Backup" Remove-PSDrive "Remote"

Note: You will need to have appropriate permissions to access the shared folder on the remote server.

Leave a Comment

Related Tag Prompts

0
0
Bash Script Requested.
1 year ago 2023-04-19 17:29:39 Christoph