Skip to content

Instantly share code, notes, and snippets.

@cderv
Last active December 20, 2023 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cderv/8657f487030efb5fd2864bf91df44d90 to your computer and use it in GitHub Desktop.
Save cderv/8657f487030efb5fd2864bf91df44d90 to your computer and use it in GitHub Desktop.
Copy files between WSL distributions
# This function, Copy-BetweenWslInstance, is designed to facilitate the copying of files between different WSL distributions.
# It requires the source and destination distributions, folders, and users as parameters.
# If the destination folder or user is not specified, the function will default to using the source folder or user.
# The function employs the wslpath utility to convert the paths into a format that is compatible with WSL.
# Finally, it utilizes the Copy-Item cmdlet to execute the file copying process.
function Copy-BetweenWslInstance {
param(
[Parameter(Mandatory=$true)]
[string]$sourceDistribution,
[Parameter(Mandatory=$true)]
[string]$sourceFolder,
[Parameter(Mandatory=$true)]
[string]$sourceUser,
[Parameter(Mandatory=$true)]
[string]$destinationDistribution,
[string]$destinationFolder = $sourceFolder,
[string]$destinationUser = $sourceUser
)
$confirmation = Read-Host "Are you sure you want to copy files from ${sourceDistribution}:${sourceFolder} as ${sourceUser} to ${destinationDistribution}:${destinationFolder} as ${destinationUser}? (Y/N)"
if ($confirmation -ne 'Y') {
Write-Host "Aborted."
return
}
Copy-Item $(wsl -d $sourceDistribution -u $sourceUser wslpath -m $sourceFolder) $(wsl -d $destinationDistribution -u $destinationUser wslpath -m $destinationFolder) -Recurse
Write-Host -ForegroundColor Blue "${sourceFolder} copied to ${destinationFolder} from ${sourceDistribution} to ${destinationDistribution}"
}
Export-ModuleMember -Function Copy-BetweenWslInstance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment