Skip to content

Instantly share code, notes, and snippets.

Last active Aug 19, 2021
What would you like to do?
git mv any folder with it's sub folder to another folder in windows powershell
# git mv a folder and sub folders in windows
function Move-GitFolder {
param (
if (Test-Path $target -PathType Leaf){
write-output "it's a file ... "
git mv $target $destination
Get-ChildItem $target -recurse |
Where-Object { ! $_.PSIsContainer } |
ForEach-Object {
$fullTargetFolder = [System.IO.Path]::GetFullPath((Join-Path (Get-Location) $target))
$fullDestinationFolder = [System.IO.Path]::GetFullPath((Join-Path (Get-Location) $destination))
$fileDestination = $_.Directory.FullName.Replace($fullTargetFolder.TrimEnd('\'), $fullDestinationFolder.TrimEnd('\'))
New-Item -ItemType Directory -Force -Path $fileDestination | Out-Null
$filePath = Join-Path $fileDestination $_.Name
git mv $_.FullName $filePath
Remove-Item -Recurse -Force $target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment