Prompt Detail:
To backup files to a shared folder on a remote server with PowerShell, you can use the following steps:
- 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.
powershellNew-PSDrive -Name "Remote" -PSProvider "FileSystem" -Root "\\RemoteServer\SharedFolder"
- Change the location to the remote folder using the
Set-Location
cmdlet. This is equivalent to navigating to the folder in Windows Explorer.
powershellSet-Location "Remote:"
- 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.
powershellCopy-Item "C:\Files\*.txt" -Destination "Remote:\Backup"
- Disconnect from the remote server using the
Remove-PSDrive
cmdlet. This closes the connection to the remote folder.
powershellRemove-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
:
powershellNew-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.
Add a comment